Derive Macro num_enum::FromPrimitive[][src]

#[derive(FromPrimitive)]
{
    // Attributes available to this derive:
    #[num_enum]
}

Implements From<Primitive> for a #[repr(Primitive)] enum.

Turning a primitive into an enum with from.

use num_enum::FromPrimitive;

#[derive(Debug, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
enum Number {
    Zero,
    #[num_enum(default)]
    NonZero,
}

fn main() {
    let zero = Number::from(0u8);
    assert_eq!(zero, Number::Zero);

    let one = Number::from(1u8);
    assert_eq!(one, Number::NonZero);

    let two = Number::from(2u8);
    assert_eq!(two, Number::NonZero);
}