From 26535a099dbe32a3781d80ca987bb28054d17de5 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 22 Nov 2022 17:43:26 +0100 Subject: [PATCH] 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. --- src/params.rs | 17 ++++++++++++++++- src/params/boolean.rs | 3 +++ src/params/enums.rs | 6 ++++++ src/params/float.rs | 3 +++ src/params/integer.rs | 3 +++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/params.rs b/src/params.rs index 44a3838c..537f332d 100644 --- a/src/params.rs +++ b/src/params.rs @@ -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; diff --git a/src/params/boolean.rs b/src/params/boolean.rs index 9cbe4231..b3b91b34 100644 --- a/src/params/boolean.rs +++ b/src/params/boolean.rs @@ -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; diff --git a/src/params/enums.rs b/src/params/enums.rs index adc2702d..817bdc3e 100644 --- a/src/params/enums.rs +++ b/src/params/enums.rs @@ -110,6 +110,9 @@ impl Display for EnumParamInner { } } +// `Params` can not be implemented outside of NIH-plug itself because `ParamPtr` is also closed +impl super::Sealed for EnumParam {} + impl Param for EnumParam { type Plain = T; @@ -188,6 +191,9 @@ impl Param for EnumParam { } } +// `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; diff --git a/src/params/float.rs b/src/params/float.rs index da2c4db6..572de863 100644 --- a/src/params/float.rs +++ b/src/params/float.rs @@ -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; diff --git a/src/params/integer.rs b/src/params/integer.rs index 79fb5d86..5f0e3b9f 100644 --- a/src/params/integer.rs +++ b/src/params/integer.rs @@ -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;