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

@@ -12,14 +12,14 @@
//! }
//!
//! pub(crate) fn create(
//! params: Pin<Arc<FooParams>>,
//! params: Arc<FooParams>,
//! editor_state: Arc<IcedState>,
//! ) -> Option<Box<dyn Editor>> {
//! create_iced_editor::<Foo>(editor_state, params)
//! }
//!
//! struct FooEditor {
//! params: Pin<Arc<FooParams>>,
//! params: Arc<FooParams>,
//! context: Arc<dyn GuiContext>,
//!
//! foo_slider_state: nih_widgets::param_slider::State,
@@ -34,7 +34,7 @@
//! impl IcedEditor for FooEditor {
//! type Executor = executor::Default;
//! type Message = Message;
//! type InitializationFlags = Pin<Arc<FooParams>>;
//! type InitializationFlags = Arc<FooParams>;
//!
//! fn new(
//! params: Self::InitializationFlags,
@@ -141,7 +141,7 @@ pub fn create_iced_editor<E: IcedEditor>(
/// A plugin editor using `iced`. This wraps around [`Application`] with the only change being that
/// the usual `new()` function now additionally takes a `Arc<dyn GuiContext>` that the editor can
/// store to interact with the parameters. The editor should have a `Pin<Arc<impl Params>>` as part
/// store to interact with the parameters. The editor should have a `Arc<impl Params>` as part
/// of their [`InitializationFlags`][Self::InitializationFlags] so it can read the current parameter
/// values. See [`Application`] for more information.
pub trait IcedEditor: 'static + Send + Sync + Sized {

View File

@@ -5,7 +5,7 @@ use atomic_refcell::AtomicRefCell;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
use nih_plug::prelude::{Param, ParamFlags, ParamPtr, Params};
@@ -58,7 +58,7 @@ pub struct GenericSlider;
pub struct GenericUi<'a, W: ParamWidget> {
state: &'a mut State<W>,
params: Pin<&'a dyn Params>,
params: Arc<dyn Params>,
width: Length,
height: Length,
@@ -85,7 +85,7 @@ where
W: ParamWidget,
{
/// Creates a new [`GenericUi`] for all provided parameters.
pub fn new(state: &'a mut State<W>, params: Pin<&'a dyn Params>) -> Self {
pub fn new(state: &'a mut State<W>, params: Arc<dyn Params>) -> Self {
Self {
state,