Procedural macros to make inter-operation between primitives and enums easier. This crate is no_std compatible.
Turning an enum into a primitive
IntoPrimitive is more type-safe than using
as will silently truncate -
num_enum only derives
From for exactly the discriminant type of the enum.
Attempting to turn a primitive into an enum with try_from
use TryFromPrimitive; use TryFrom;
Unsafely turning a primitive into an enum with from_unchecked
If you're really certain a conversion will succeed, and want to avoid a small amount of overhead, you can use unsafe
code to do this conversion. Unless you have data showing that the match statement generated in the
try_from above is a
bottleneck for you, you should avoid doing this, as the unsafe code has potential to cause serious memory issues in
use UnsafeFromPrimitive; unsafe
Some enum values may be composed of complex expressions, for example:
To cut down on compile time, these are not supported by default, but if you enable the
complex-expressions feature of your dependency on
num_enum, these should start working.