Trait nih_plug::params::enums::Enum

source ·
pub trait Enum {
    // Required methods
    fn variants() -> &'static [&'static str];
    fn ids() -> Option<&'static [&'static str]>;
    fn to_index(self) -> usize;
    fn from_index(index: usize) -> Self;
}
Expand description

An enum usable with EnumParam. This trait can be derived. Variants are identified either by a stable id (see below), or if those are not set then they are identifier by their declaration order. If you don’t provide IDs then you can freely rename the variant names, but reordering them will break compatibility with existing presets. The variant’s name is used as the display name by default. If you want to override this, for instance, because it needs to contain spaces, then you can use the #[name = "..."] attribute:

#[derive(Enum)]
enum Foo {
    Bar,
    Baz,
    #[name = "Contains Spaces"]
    ContainsSpaces,
}

IDs can be added by adding the #[id = "..."] attribute to each variant:

#[derive(Enum)]
enum Foo {
    #[id = "bar"],
    Bar,
    #[id = "baz"],
    Baz,
    #[id = "contains-spaces"],
    #[name = "Contains Spaces"]
    ContainsSpaces,
}

You can safely move from not using IDs to using IDs without breaking patches, but you cannot go back to not using IDs after that.

Required Methods§

source

fn variants() -> &'static [&'static str]

The human readable names for the variants. These are displayed in the GUI or parameter list, and also used for parsing text back to a parameter value. The length of this slice determines how many variants there are.

source

fn ids() -> Option<&'static [&'static str]>

Optional identifiers for each variant. This makes it possible to reorder variants while maintaining save compatibility (automation will still break of course). The length of this slice needs to be equal to variants().

source

fn to_index(self) -> usize

Get the variant index (which may not be the same as the discriminator) corresponding to the active variant. The index needs to correspond to the name in variants().

source

fn from_index(index: usize) -> Self

Get the variant corresponding to the variant with the same index in variants(). This must always return a value. If the index is out of range, return the first variant.

Implementors§