of_variant

Macro of_variant 

Source
macro_rules! of_variant {
    ($ty:ident $(::<$($ty_arg:ty),*>)? :: $variant:ident) => { ... };
    ($ty:ident $(::<$($ty_arg:ty),*>)? :: $variant:ident (..)) => { ... };
    ($ty:ident $(::<$($ty_arg:ty),*>)? :: $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 String.

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 checks that the variant exists on the given enum type. If either the type or variant is renamed via refactoring tools, the macro call will be updated accordingly.

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.

ยง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");