macro_rules! create_indexed_valued_enum { ( $(#[$metadata:meta])* $(##[features($($features:tt),*)])? $visibility:vis enum $enum_name:ident valued as $value_type:ty; $($(#[$variants_metadata:meta])* $variants:ident, $values:expr $(;unnamed_field_initializers $($unnamed_field_initializers:expr),+)? $(;named_field_initializers $($named_field_name:ident $named_field_value:expr),+)? ),+ $(,)? ) => { ... }; ( impl traits $enum_name:ident $value_type:ty; $($variants:ident, $values:expr $(;unnamed_field_initializers $($unnamed_field_initializers:expr),+ ;)? $(;named_field_initializers $($named_field_name:ident $(:)? $named_field_value:expr),+ ;)? ),+ ) => { ... }; (process features $enum_name:ident, $value_type:ty; $($features:tt);*) => { ... }; (process feature $enum_name:ident, $value_type:ty; Delegators) => { ... }; (process feature $enum_name:ident, $value_type:ty; ValueToVariantDelegators) => { ... }; (process feature $enum_name:ident, $value_type:ty; DerefToValue) => { ... }; (process feature $enum_name:ident, $value_type:ty; Clone) => { ... }; (process feature $enum_name:ident, $value_type:ty; Serialize) => { ... }; (process feature $enum_name:ident, $value_type:ty; Deserialize) => { ... }; (process feature $enum_name:ident, $value_type:ty; NanoSerBin) => { ... }; (process feature $enum_name:ident, $value_type:ty; NanoDeBin) => { ... }; (process feature $enum_name:ident, $value_type:ty; SerJson) => { ... }; (process feature $enum_name:ident, $value_type:ty; NanoDeJson) => { ... }; }
Expand description
Produces an enum implementing the Indexed and Valued traits, meaning the enum’s variants can produce unique numbers of usize to identify each variant through Indexed::discriminant, and get back those variants through Indexed::from_discriminant, and similar to it, each variant has a value that can be taken from Valued::value, where the variant can be taken back* from that value through Valued::value_to_variant.
*Just if the value isn’t repeated.
Being a macro by rules, you only need to follow this pattern:
create_indexed_valued_enum!{
Your metadata //Like ‘#[derive(…)]’, this is optional
##[features(Feature1, Feature2, …)] // this is optional, but it needs two octothorpes
Visibility enum Enum’s name values as TypeOfValue;
Variant1’s metadata //this is optional
Variant1, Value1,
Variant2’s metadata //this is optional
Variant2, Value2,
…
VariantN’s metadata //this is optional
VariantN, ValueN
}
On each of these fields you can indicate different parameters to change the implementation of the enum:
- EnumsName: Name the enum will have.
- TypeOfValue: type of the values the variant’s resolve to.
- Pairs of Variant, Value: Name of the variant’s to create along to the name they resolve to, the values must be const and have ’static lifetime.
- Features: List of specific implementations you want your enum to use, see the section
- Features: List of specific implementations you want your enum to use, you can find a list of them in the documentation of crate -> Section: Extra features.
Note: You can write metadata (Such as #[derive(…)]) before each pair of Variant, Value, and also before the enum, but it is required that the ##[features(…)] is the last of the metadatas as this is not another metadata (hence the double hashtag to denote it)/// A simple example would look like:
use indexed_valued_enums::create_indexed_valued_enum;
create_indexed_valued_enum! {
//Defines the enum and the value type it resolves to
pub enum MyOtherNumber valued as &'static str;
//Defines every variant and their value, note that values must be const
Zero, "Zero position",
First, "First position",
Second, "Second position",
Third, "Third position"
}
A more complex example would look like:
use indexed_valued_enums::create_indexed_valued_enum;
create_indexed_valued_enum! {
#[doc="This is a custom enum that can get values of &'static str!"]
//This enum derives certain traits, although you don't need to write this
#[derive(Hash, Ord, PartialOrd, Eq, PartialEq, Debug)]
//Gives a list of features that are decomposed functions for specific behaviours, you have
//more details about them down below
##[features(Clone, DerefToValue, Delegators, ValueToVariantDelegators)]
//Defines the enum and the value type it resolves to
pub enum MyOtherNumber valued as &'static str;
//Defines every variant and their value, note that values must be const
Zero, "Zero position",
First, "First position",
Second, "Second position",
Third, "Third position"
}