macro_rules! of_variant {
(Self:: $variant:ident) => { ... };
(Self:: $variant:ident (..)) => { ... };
(Self:: $variant:ident {..}) => { ... };
($ty:ident :: $variant:ident) => { ... };
($ty:ident :: $variant:ident (..)) => { ... };
($ty:ident :: $variant:ident {..}) => { ... };
(<$ty:ty> :: $variant:ident) => { ... };
(<$ty:ty> :: $variant:ident (..)) => { ... };
(<$ty:ty> :: $variant:ident {..}) => { ... };
}Expand description
Get the name of the given enum variant as a &'static str.
This macro resolves Self to the appropriate type when used inside an impl block.
This macros supports both unit variants, tuple variants and struct variants. See examples for syntax for each variant type.
This macro currently expects only simple type identifiers like Type::field.
Support for more complex types requires the experimental feature more_qualified_paths
(issue #86935 https://github.com/rust-lang/rust/issues/86935) to be stabilized (or
enabled via #![feature(more_qualified_paths)] if using a nightly compiler).
ยงExamples
enum MyEnum {
UnitVariant,
TupleVariant(u32, String),
StructVariant { field: u32 },
}
assert_eq!(pretty_name::of_variant!(MyEnum::UnitVariant), "MyEnum::UnitVariant");
assert_eq!(pretty_name::of_variant!(MyEnum::TupleVariant(..)), "MyEnum::TupleVariant");
assert_eq!(pretty_name::of_variant!(MyEnum::StructVariant {..}), "MyEnum::StructVariant");