Macro gen_atomic_enum

Source
macro_rules! gen_atomic_enum {
    ($name:ident, $b_ty:ty: $($val:ident: $num:expr)*) => { ... };
}
Expand description

This macro can be used to generate a C like enumeration, which automaticly implements impl From<YourStruct> for <youre base type> { ... } and Atomize. You must implement the trait impl TryFrom<youre base type> for YourStruct to use the enumeration.

§Params

  1. The name, which the enumeration
  2. The underlying type (u8, u16, u32, u64, usize)
  3. List of, EnumerationField: number

§Example

use atomic_enums::gen_atomic_enum;

gen_atomic_enum!(State, u32:
    Running: 2
    Paused: 3
);

impl TryFrom<u32> for State {
    type Error = ();

    fn try_from(v: u32) -> Result<Self, Self::Error> {
        match v {
            2 => Ok(Self::Running),
            3 => Ok(Self::Paused),
            _ => Err(()),
        }
    }
}

assert_eq!(State::Running as u32, 2);
assert_eq!(State::Paused as u32, 3);