| 1 |
[](https://gitlab.com/cardoe/enum-primitive-derive/commits/master) |
| 2 |
[]() |
| 3 |
[](https://docs.rs/enum-primitive-derive) |
| 4 |
[](https://crates.io/crates/enum-primitive-derive) |
| 5 |
[](https://crates.io/crates/enum-primitive-derive) |
| 6 |
[](https://crates.io/crates/enum-primitive-derive) |
| 7 |
|
| 8 |
This is a custom derive, using procedural macros, implementation of |
| 9 |
[enum_primitive](https://crates.io/crates/enum_primitive). |
| 10 |
|
| 11 |
MSRV is 1.34.0 |
| 12 |
|
| 13 |
## Documentation |
| 14 |
|
| 15 |
[https://docs.rs/enum-primitive-derive/](https://docs.rs/enum-primitive-derive/) |
| 16 |
|
| 17 |
## Usage |
| 18 |
|
| 19 |
Add the following to `Cargo.toml`: |
| 20 |
|
| 21 |
``` |
| 22 |
[dependencies] |
| 23 |
enum-primitive-derive = "^0.1" |
| 24 |
num-traits = "^0.1" |
| 25 |
``` |
| 26 |
|
| 27 |
Then to your code add: |
| 28 |
|
| 29 |
```rust |
| 30 |
#[macro_use] |
| 31 |
extern crate enum_primitive_derive; |
| 32 |
extern crate num_traits; |
| 33 |
|
| 34 |
#[derive(Primitive)] |
| 35 |
enum Variant { |
| 36 |
Value = 1, |
| 37 |
Another = 2, |
| 38 |
} |
| 39 |
``` |
| 40 |
|
| 41 |
To be really useful you need `use num_traits::FromPrimitive` or |
| 42 |
`use num_traits::ToPrimitive` or both. You will then be able to |
| 43 |
use |
| 44 |
[num_traits::FromPrimitive](https://rust-num.github.io/num/num/trait.FromPrimitive.html) |
| 45 |
and/or |
| 46 |
[num_traits::ToPrimitive](https://rust-num.github.io/num/num/trait.ToPrimitive.html) |
| 47 |
on your enum. |
| 48 |
|
| 49 |
## Full Example |
| 50 |
|
| 51 |
```rust |
| 52 |
#[macro_use] |
| 53 |
extern crate enum_primitive_derive; |
| 54 |
extern crate num_traits; |
| 55 |
|
| 56 |
use num_traits::{FromPrimitive, ToPrimitive}; |
| 57 |
|
| 58 |
#[derive(Primitive)] |
| 59 |
enum Foo { |
| 60 |
Bar = 32, |
| 61 |
Dead = 42, |
| 62 |
Beef = 50, |
| 63 |
} |
| 64 |
|
| 65 |
fn main() { |
| 66 |
assert_eq!(Foo::from_i32(32), Some(Foo::Bar)); |
| 67 |
assert_eq!(Foo::from_i32(42), Some(Foo::Dead)); |
| 68 |
assert_eq!(Foo::from_i64(50), Some(Foo::Beef)); |
| 69 |
assert_eq!(Foo::from_isize(17), None); |
| 70 |
|
| 71 |
let bar = Foo::Bar; |
| 72 |
assert_eq!(bar.to_i32(), Some(32)); |
| 73 |
|
| 74 |
let dead = Foo::Dead; |
| 75 |
assert_eq!(dead.to_isize(), Some(42)); |
| 76 |
} |
| 77 |
``` |
| 78 |
|
| 79 |
# Complex Example |
| 80 |
|
| 81 |
In this case we attempt to use values created by |
| 82 |
[bindgen](https://crates.io/crates/bindgen). |
| 83 |
|
| 84 |
```rust |
| 85 |
#[macro_use] |
| 86 |
extern crate enum_primitive_derive; |
| 87 |
extern crate num_traits; |
| 88 |
|
| 89 |
use num_traits::{FromPrimitive, ToPrimitive}; |
| 90 |
|
| 91 |
pub const ABC: ::std::os::raw::c_uint = 1; |
| 92 |
pub const DEF: ::std::os::raw::c_uint = 2; |
| 93 |
pub const GHI: ::std::os::raw::c_uint = 4; |
| 94 |
|
| 95 |
#[derive(Clone, Copy, Debug, Eq, PartialEq, Primitive)] |
| 96 |
enum BindGenLike { |
| 97 |
ABC = ABC as isize, |
| 98 |
DEF = DEF as isize, |
| 99 |
GHI = GHI as isize, |
| 100 |
} |
| 101 |
|
| 102 |
fn main() { |
| 103 |
assert_eq!(BindGenLike::from_isize(4), Some(BindGenLike::GHI)); |
| 104 |
assert_eq!(BindGenLike::from_u32(2), Some(BindGenLike::DEF)); |
| 105 |
assert_eq!(BindGenLike::from_u32(8), None); |
| 106 |
|
| 107 |
let abc = BindGenLike::ABC; |
| 108 |
assert_eq!(abc.to_u32(), Some(1)); |
| 109 |
} |
| 110 |
``` |