Expand description
Type-safe conversions between enums and primitive numbers.
- Crate
::num_enum. - docs.rs
- crates.io
- GitHub
num_enum provides procedural macros for deriving type-safe conversions
between Rust enums and primitive numeric types.
While Rust’s as operator can convert enums to numbers,
it can silently truncate values and doesn’t provide safe conversions
in the reverse direction.
num_enum fills this gap with derive macros that generate
conversion implementations with proper error handling.
The primary derives are IntoPrimitive for converting enums to numbers,
and TryFromPrimitive for fallible conversion from numbers to enums.
For cases where all numeric values map to enum variants,
FromPrimitive provides infallible conversion,
and UnsafeFromPrimitive offers unsafe transmutation for performance-critical code.
§Examples
Basic conversion between enums and numbers:
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[derive(Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
enum Status {
Success = 0,
Warning = 1,
Error = 2,
}
// Convert enum to number.
let code: u8 = Status::Warning.into();
assert_eq!(code, 1);
// Convert number to enum.
let status = Status::try_from(2).unwrap();
assert_eq!(status, Status::Error);
// Invalid conversions return an error.
assert!(Status::try_from(99).is_err());Structs§
Traits§
Derive Macros§
- Default
- Implements
core::default::Defaultfor a#[repr(Primitive)] enum. - From
Primitive - Implements
From<Primitive>for a#[repr(Primitive)] enum. - Into
Primitive - Implements
Into<Primitive>for a#[repr(Primitive)] enum. - TryFrom
Primitive - Implements
TryFrom<Primitive>for a#[repr(Primitive)] enum. - Unsafe
From Primitive - Generates a
unsafe fn unchecked_transmute_from(number: Primitive) -> Selfassociated function.