Procedural macros to make inter-operation between primitives and enums easier.
```rust extern crate numenum; use numenum::IntoPrimitive;
enum Number { Zero, One, }
fn convert() { let zero: u8 = Number::Zero.into(); assert_eq!(zero, 0u8); } ```
num_enum
's IntoPrimitive
is more type-safe than using as
, because as
will silently truncate - num_enum
only derives From
for exactly the discriminant type of the enum.
```rust
extern crate num_enum;
use num_enum::TryFromPrimitive; use std::convert::TryInto;
enum Number { Zero, One, }
fn convert() { let zero: Number = 0u8.tryinto().unwrap(); asserteq!(zero, Ok(Number::Zero));
let three: Result<Number, String> = 3u8.try_into();
assert_eq!(three, Err("No value in enum Number for value 3".to_owned()));
} ```
Unfortunately, try_from
is not yet stable (see https://github.com/rust-lang/rust/issues/33417), so num_enum
can also derive a custom trait which can be used in stable rust. It's a little ugly, but it does the job! It adds a function called try_into_{enum_name}
which you can use. If you're converting outside of the module where the enum was defined, you may also need to use your_module::TryInto{enum_name};
.
```rust extern crate num_enum;
use num_enum::CustomTryInto;
enum Number { Zero, One, }
fn convert() {
let zero: Result
let three: Result<Number, String> = 3u8.try_into_Number();
assert_eq!(three, Err("No value in enum Number for value 3".to_owned()));
} ```
Some enum values may be composed of complex expressions, for example:
rust
enum Number {
Zero: (0, 1).0,
One: (0, 1),1,
}
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.