Rework Params trait API with Arc instead of Pin

This is a breaking change requiring a small change to plugin
implementations.

The reason why `Pin<&dyn Params>` was used was more as a hint to
indicate that the object must last for the plugin's lifetime, but `Pin`
doesn't enforce that. It also makes the APIs a lot more awkward.
Requiring the use of `Arc` fixes the following problems:

- When storing the params object in the wrapper, the `ParamPtr`s are
  guaranteed to be stable.
- This makes it possible to access the `Params` object without acquiring
  a lock on the plugin, this is very important for implementing
  plugin-side preset management.
- It enforces immutability on the `Params` object.
- And of course the API is much nicer without a bunch of unsafe code to
  work around Pin's limitations.
This commit is contained in:
Robbert van der Helm
2022-04-07 15:31:46 +02:00
parent 7cc05fce9a
commit 083885a40c
24 changed files with 105 additions and 115 deletions

View File

@@ -18,7 +18,6 @@ use nih_plug::prelude::Editor;
use nih_plug_vizia::vizia::*;
use nih_plug_vizia::widgets::*;
use nih_plug_vizia::{assets, create_vizia_editor, ViziaState};
use std::pin::Pin;
use std::sync::Arc;
use crate::CrispParams;
@@ -28,7 +27,7 @@ const POINT_SCALE: f32 = 0.75;
#[derive(Lens)]
struct Data {
params: Pin<Arc<CrispParams>>,
params: Arc<CrispParams>,
}
impl Model for Data {}
@@ -39,7 +38,7 @@ pub(crate) fn default_state() -> Arc<ViziaState> {
}
pub(crate) fn create(
params: Pin<Arc<CrispParams>>,
params: Arc<CrispParams>,
editor_state: Arc<ViziaState>,
) -> Option<Box<dyn Editor>> {
create_vizia_editor(editor_state, move |cx| {

View File

@@ -20,7 +20,6 @@ extern crate nih_plug;
use nih_plug::prelude::*;
use nih_plug_vizia::ViziaState;
use pcg::Pcg32iState;
use std::pin::Pin;
use std::sync::Arc;
mod editor;
@@ -44,7 +43,7 @@ const MAX_FILTER_FREQUENCY: f32 = 22_000.0;
/// white (or filtered) noise. That other copy of the sound may have a low-pass filter applied to it
/// since this effect just turns into literal noise at high frequencies.
struct Crisp {
params: Pin<Arc<CrispParams>>,
params: Arc<CrispParams>,
editor_state: Arc<ViziaState>,
/// Needed for computing the filter coefficients.
@@ -126,7 +125,7 @@ enum StereoMode {
impl Default for Crisp {
fn default() -> Self {
Self {
params: Arc::pin(CrispParams::default()),
params: Arc::new(CrispParams::default()),
editor_state: editor::default_state(),
sample_rate: 1.0,
@@ -308,8 +307,8 @@ impl Plugin for Crisp {
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
fn params(&self) -> Pin<&dyn Params> {
self.params.as_ref()
fn params(&self) -> Arc<dyn Params> {
self.params.clone()
}
fn editor(&self) -> Option<Box<dyn Editor>> {