Derive Macro strum::EnumDiscriminants
source · [−]#[derive(EnumDiscriminants)]
{
// Attributes available to this derive:
#[strum]
#[strum_discriminants]
}
Expand description
Generate a new type with only the discriminant names.
Given an enum named MyEnum
, generates another enum called MyEnumDiscriminants
with the same variants, without any data fields.
This is useful when you wish to determine the variant of an enum from a String, but the variants contain any
non-Default
fields. By default, the generated enum has the following derives:
Clone, Copy, Debug, PartialEq, Eq
. You can add additional derives using the
#[strum_discriminants(derive(AdditionalDerive))]
attribute.
// Bring trait into scope
use std::str::FromStr;
use strum::IntoEnumIterator;
use strum_macros::{EnumDiscriminants, EnumIter, EnumString};
#[derive(Debug)]
struct NonDefault;
// simple example
#[derive(Debug, EnumDiscriminants)]
#[strum_discriminants(derive(EnumString))]
enum MyEnum {
Variant0(NonDefault),
Variant1 { a: NonDefault },
}
// You can rename the generated enum using the `#[strum_discriminants(name(OtherName))]` attribute:
#[derive(Debug, EnumDiscriminants)]
#[strum_discriminants(derive(EnumIter))]
#[strum_discriminants(name(MyVariants))]
enum MyEnumR {
Variant0(bool),
Variant1 { a: bool },
}
// test simple example
assert_eq!(
MyEnumDiscriminants::Variant0,
MyEnumDiscriminants::from_str("Variant0").unwrap()
);
// test rename example combined with EnumIter
assert_eq!(
vec![MyVariants::Variant0, MyVariants::Variant1],
MyVariants::iter().collect::<Vec<_>>()
);
It is also possible to specify the visibility (e.g. pub
/pub(crate)
/etc.)
of the generated enum. By default, the generated enum inherits the
visibility of the parent enum it was generated from.
use strum_macros::EnumDiscriminants;
// You can set the visibility of the generated enum using the `#[strum_discriminants(vis(..))]` attribute:
mod inner {
use strum_macros::EnumDiscriminants;
#[derive(Debug, EnumDiscriminants)]
#[strum_discriminants(vis(pub))]
#[strum_discriminants(name(PubDiscriminants))]
enum PrivateEnum {
Variant0(bool),
Variant1 { a: bool },
}
}
// test visibility example, `PrivateEnum` should not be accessible here
assert_ne!(
inner::PubDiscriminants::Variant0,
inner::PubDiscriminants::Variant1,
);