Derive Macro EnumRepr

Source
#[derive(EnumRepr)]
{
    // Attributes available to this derive:
    #[enum_repr]
}
Expand description

automatically implements [std::convert::From] and [std::convert::Into] for enums

  • Ideal for managing Type
  • Easy to understand constants

§Example only derive

  • require #[repr(u8)] or #[repr(u16)] or …
  • default is require explicit
use more_convert::EnumRepr;
#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[repr(u16)]
pub enum Test {
    Zero = 0,
    Three = 3,
    Four = 4,
}

assert_eq!(0u16, Test::Zero.into());
assert_eq!(3u16, Test::Three.into());
assert_eq!(4u16, Test::Four.into());

assert_eq!(0u16.try_into(), Ok(Test::Zero));
assert_eq!(3u16.try_into(), Ok(Test::Three));
assert_eq!(4u16.try_into(), Ok(Test::Four));