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

@@ -1,6 +1,6 @@
//! Generic UIs for NIH-plug using VIZIA.
use std::{ops::Deref, pin::Pin};
use std::sync::Arc;
use nih_plug::prelude::{ParamFlags, ParamPtr, Params};
use vizia::*;
@@ -25,14 +25,12 @@ impl GenericUi {
/// })
/// .width(Percentage(100.0));
///```
pub fn new<L, PsPtr, Ps>(cx: &mut Context, params: L) -> Handle<'_, GenericUi>
pub fn new<L, Ps>(cx: &mut Context, params: L) -> Handle<'_, GenericUi>
where
L: Lens<Target = Pin<PsPtr>> + Copy,
PsPtr: 'static + Deref<Target = Ps>,
Ps: Params,
L: Lens<Target = Arc<Ps>> + Copy,
Ps: Params + 'static,
{
// Basic styling is done in the `theme.css` style sheet
// TODO: Scrolling
Self::new_custom(cx, params, |cx, param_ptr| {
HStack::new(cx, |cx| {
// Align this on the right
@@ -70,15 +68,14 @@ impl GenericUi {
/// Creates a new [`GenericUi`] for all provided parameters using a custom closure that receives
/// a function that should draw some widget for each parameter.
pub fn new_custom<L, PsPtr, Ps>(
pub fn new_custom<L, Ps>(
cx: &mut Context,
params: L,
mut make_widget: impl FnMut(&mut Context, ParamPtr),
) -> Handle<Self>
where
L: Lens<Target = Pin<PsPtr>> + Copy,
PsPtr: 'static + Deref<Target = Ps>,
Ps: Params,
L: Lens<Target = Arc<Ps>> + Copy,
Ps: Params + 'static,
{
// Basic styling is done in the `theme.css` style sheet
Self.build2(cx, |cx| {