num_enum

Derive Macro FromPrimitive

source
#[derive(FromPrimitive)]
{
    // Attributes available to this derive:
    #[num_enum]
    #[default]
    #[catch_all]
}
Expand description

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,
}

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);