1
0
Fork 0

Seal the Params trait

This avoids situations like #41 where people try to implement the trait
only to find out that you simply can't.
This commit is contained in:
Robbert van der Helm 2022-11-22 17:43:26 +01:00
parent f0c6ce6e71
commit 26535a099d
5 changed files with 31 additions and 1 deletions

View file

@ -53,12 +53,27 @@ bitflags::bitflags! {
}
}
// See https://rust-lang.github.io/api-guidelines/future-proofing.html for more information
mod sealed {
/// Dummy trait to prevent [`Param`] from being implemented outside of NIH-plug. This is not
/// possible because of the way `ParamPtr` works, so it's best to just make it flat out impossible.
pub trait Sealed {}
}
pub(crate) use sealed::Sealed;
/// Describes a single parameter of any type. Most parameter implementations also have a field
/// called `value` that and a field called `smoothed`. The former stores the latest unsmoothed
/// value, and the latter can be used to access the smoother. These two fields should be used in DSP
/// code to either get the parameter's current (smoothed) value. In UI code the getters from this
/// trait should be used instead.
pub trait Param: Display {
///
/// # Sealed
///
/// This trait cannot be implemented outside of NIH-plug itself. If you want to create new
/// abstractions around parameters, consider wrapping them in a struct instead. Then use the
/// `#[nested(id_prefix = "foo")]` syntax from the [`Params`] trait to reuse that wrapper in
/// multiple places.
pub trait Param: Display + sealed::Sealed {
/// The plain parameter type.
type Plain: PartialEq;

View file

@ -61,6 +61,9 @@ impl Display for BoolParam {
}
}
// `Params` can not be implemented outside of NIH-plug itself because `ParamPtr` is also closed
impl super::Sealed for BoolParam {}
impl Param for BoolParam {
type Plain = bool;

View file

@ -110,6 +110,9 @@ impl Display for EnumParamInner {
}
}
// `Params` can not be implemented outside of NIH-plug itself because `ParamPtr` is also closed
impl<T: Enum + PartialEq> super::Sealed for EnumParam<T> {}
impl<T: Enum + PartialEq> Param for EnumParam<T> {
type Plain = T;
@ -188,6 +191,9 @@ impl<T: Enum + PartialEq> Param for EnumParam<T> {
}
}
// `Params` can not be implemented outside of NIH-plug itself because `ParamPtr` is also closed
impl super::Sealed for EnumParamInner {}
impl Param for EnumParamInner {
type Plain = i32;

View file

@ -86,6 +86,9 @@ impl Display for FloatParam {
}
}
// `Params` can not be implemented outside of NIH-plug itself because `ParamPtr` is also closed
impl super::Sealed for FloatParam {}
impl Param for FloatParam {
type Plain = f32;

View file

@ -78,6 +78,9 @@ impl Display for IntParam {
}
}
// `Params` can not be implemented outside of NIH-plug itself because `ParamPtr` is also closed
impl super::Sealed for IntParam {}
impl Param for IntParam {
type Plain = i32;