Derive Macro num_enum::TryFromPrimitive
#[derive(TryFromPrimitive)]
{
// Attributes available to this derive:
#[num_enum]
}
Expand description
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,
}
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`",
);