#[derive(VariantDescriptors)]
Expand description
A derive procedural macro for the VariantDescriptors
trait.
This macro generates an implementation of the VariantDescriptors
trait for
an enum. The trait provides two methods:
-
variant_names_with_token_counts
: Returns aVec
containing tuples, each with a string representation of a variant’s name and its token count. -
variant_name_with_token_count
: Takes an enum variant as input and returns a tuple with the variant’s name as a string and its token count.
Note: This macro will panic if it is used on anything other than an enum.
§Usage
#[derive(VariantDescriptors)]
enum MyEnum {
Variant1,
Variant2,
}
This will generate the following:
impl VariantDescriptors for MyEnum {
fn variant_names_with_token_counts() -> Vec<(String, usize)> {
vec![
(String::from("Variant1"), /* token count of "Variant1" */),
(String::from("Variant2"), /* token count of "Variant2" */),
]
}
fn variant_name_with_token_count(&self) -> (String, usize) {
match self {
Self::Variant1 => (String::from("Variant1"), /* token count of "Variant1" */),
Self::Variant2 => (String::from("Variant2"), /* token count of "Variant2" */),
}
}
}
The actual token count is computed during compile time using the
calculate_token_count
function.