Attribute Macro auto_enum::auto_enum

source ·
#[auto_enum]
Expand description

Generates methods for converting an enum value to and from its integer representation safely. Defaults to u32 representation.

Additional parameters

#[auto_enum({representation} [, checked])]

{representation} may be any unsigned integral type from 8 to 64 (including usize).

If the checked flag is specified it will generate an implementation of checked_enum::CheckedEnum. The crate must be included to use this option.

Basic usage

#[auto_enum]
pub enum Foo {
    Bar,
    Baz,
}
 
assert_eq!(Foo::Bar.to_u32(), 0);
assert_eq!(Foo::Baz.to_u32(), 1);
 
assert_eq!(Foo::from_u32(0), Some(Foo::Bar));
assert_eq!(Foo::from_u32(1), Some(Foo::Baz));
assert_eq!(Foo::from_u32(2), None);