[][src]Attribute Macro int_enum::int_enum

#[int_enum]

Attribute macro taking the integer type for the enum.

Examples

Basic usage:

use int_enum::{int_enum, IntEnum};

#[int_enum(u8)]
#[derive(PartialEq, Eq)]
pub enum SmallInt {
    One = 1,
    Two = 2,
}

assert_eq!(SmallInt::One.to_int(), 1);

assert_eq!(SmallInt::from_int(2), Ok(SmallInt::Two));
assert!(SmallInt::from_int(5).is_err());

Serde support (requires feature serialize):

use int_enum::{int_enum, IntEnum};

#[int_enum(u8)]
enum Num {
    One = 1,
    Two = 2,
    Three = 3,
}

assert_eq!("1", serde_json::to_string(&Num::One)?);
assert_eq!("2", serde_json::to_string(&Num::Two)?);
assert_eq!("3", serde_json::to_string(&Num::Three)?);

assert!(serde_json::from_str::<Num>("4").is_err());