| Crates.io | enum_primitive |
| lib.rs | enum_primitive |
| version | 0.1.1 |
| created_at | 2015-04-07 07:55:53.581752+00 |
| updated_at | 2017-01-09 01:07:32.180507+00 |
| description | Macro to generate num::FromPrimitive instances for enum that works in Rust 1.0 |
| homepage | https://github.com/andersk/enum_primitive-rs |
| repository | https://github.com/andersk/enum_primitive-rs.git |
| max_upload_size | |
| id | 1795 |
| size | 20,877 |
This crate exports a macro enum_from_primitive! that wraps an
enum declaration and automatically adds an implementation of
num::FromPrimitive (reexported here), to allow conversion from
primitive integers to the enum. It therefore provides an
alternative to the built-in #[derive(FromPrimitive)], which
requires the unstable std::num::FromPrimitive and is disabled in
Rust 1.0.
https://andersk.github.io/enum_primitive-rs/enum_primitive/
Add the following to your Cargo.toml file:
[dependencies]
enum_primitive = "*"
Import the crate using #[macro_use] extern crate enum_primitive, and
wrap your enum declaration inside the enum_from_primitive! macro.
#[macro_use] extern crate enum_primitive;
extern crate num;
use num::FromPrimitive;
enum_from_primitive! {
#[derive(Debug, PartialEq)]
enum FooBar {
Foo = 17,
Bar = 42,
Baz,
}
}
fn main() {
assert_eq!(FooBar::from_i32(17), Some(FooBar::Foo));
assert_eq!(FooBar::from_i32(42), Some(FooBar::Bar));
assert_eq!(FooBar::from_i32(43), Some(FooBar::Baz));
assert_eq!(FooBar::from_i32(91), None);
}