Derive Macro num_enum::TryFromPrimitive[][src]

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

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

Attempting to turn a primitive into an enum with try_from.

use num_enum::TryFromPrimitive;
use std::convert::TryFrom;

#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u8)]
enum Number {
    Zero,
    One,
}

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

    let three = Number::try_from(3u8);
    assert_eq!(
        three.unwrap_err().to_string(),
        "No discriminant in enum `Number` matches the value `3`",
    );
}