1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
num_enum
========
Procedural macros to make inter-operation between primitives and enums easier.
This crate is no_std compatible.
[](https://crates.io/crates/num_enum)
[](https://docs.rs/num_enum)
[](https://travis-ci.org/illicitonion/num_enum)
Turning an enum into a primitive
--------------------------------
```rust
use num_enum::IntoPrimitive;
#[derive(IntoPrimitive)]
#[repr(u8)]
enum Number {
Zero,
One,
}
fn main() {
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.
Attempting to turn a primitive into an enum with try_from
----------------------------------------------
```rust
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u8)]
enum Number {
Zero,
One,
}
fn main() {
let zero = Number::try_from(0u8);
assert_eq!(zero, Ok(Number::Zero));
let three = Number::try_from(3u8);
assert_eq!(
three.unwrap_err().to_string(),
"No discriminant in enum `Number` matches the value `3`",
);
}
```
Variant alternatives
---------------
Sometimes a single enum variant might be representable by multiple numeric values.
The `#[num_enum(alternatives = [..])]` attribute allows you to define additional value alternatives for individual variants.
(The behavior of `IntoPrimitive` is unaffected by this attribute, it will always return the canonical value.)
```rust
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u8)]
enum Number {
Zero = 0,
#[num_enum(alternatives = [2])]
OneOrTwo = 1,
}
fn main() {
let zero = Number::try_from(0u8);
assert_eq!(zero, Ok(Number::Zero));
let one = Number::try_from(1u8);
assert_eq!(one, Ok(Number::OneOrTwo));
let two = Number::try_from(2u8);
assert_eq!(two, Ok(Number::OneOrTwo));
let three = Number::try_from(3u8);
assert_eq!(
three.unwrap_err().to_string(),
"No discriminant in enum `Number` matches the value `3`",
);
}
```
Range expressions are also supported for alternatives, but this requires enabling the `complex-expressions` feature:
```rust
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u8)]
enum Number {
Zero = 0,
#[num_enum(alternatives = [2..16])]
Some = 1,
#[num_enum(alternatives = [17, 18..=255])]
Many = 16,
}
fn main() {
let zero = Number::try_from(0u8);
assert_eq!(zero, Ok(Number::Zero));
let some = Number::try_from(15u8);
assert_eq!(some, Ok(Number::Some));
let many = Number::try_from(255u8);
assert_eq!(many, Ok(Number::Many));
}
```
Default variant
---------------
Sometimes it is desirable to have an `Other` variant in an enum that acts as a kind of a wildcard matching all the value not yet covered by other variants.
The `#[num_enum(default)]` attribute allows you to mark variant as the default.
(The behavior of `IntoPrimitive` is unaffected by this attribute, it will always return the canonical value.)
```rust
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u8)]
enum Number {
Zero = 0,
#[num_enum(default)]
NonZero = 1,
}
fn main() {
let zero = Number::try_from(0u8);
assert_eq!(zero, Ok(Number::Zero));
let one = Number::try_from(1u8);
assert_eq!(one, Ok(Number::NonZero));
let two = Number::try_from(2u8);
assert_eq!(two, Ok(Number::NonZero));
}
```
Safely turning a primitive into an exhaustive enum with from_primitive
-------------------------------------------------------------
If your enum has all possible primitive values covered, you can derive `FromPrimitive` for it (which auto-implement stdlib's `From`):
You can cover all possible values by:
* Having variants for every possible value
* Having a variant marked `#[num_enum(default)]`
* Having a variant marked `#[num_enum(catch_all)]`
* Having `#[num_enum(alternatives = [...])`s covering values not covered by a variant.
```rust
use num_enum::FromPrimitive;
#[derive(Debug, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
enum Number {
Zero,
#[num_enum(default)]
NonZero,
}
fn main() {
assert_eq!(
Number::Zero,
Number::from(0_u8),
);
assert_eq!(
Number::NonZero,
Number::from(1_u8),
);
}
```
Catch-all variant
-----------------
Sometimes it is desirable to have an `Other` variant which holds the otherwise un-matched value as a field.
The `#[num_enum(catch_all)]` attribute allows you to mark at most one variant for this purpose. The variant it's applied to must be a tuple variant with exactly one field matching the `repr` type.
```rust
use num_enum::FromPrimitive;
use std::convert::TryFrom;
#[derive(Debug, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
enum Number {
Zero = 0,
#[num_enum(catch_all)]
NonZero(u8),
}
fn main() {
let zero = Number::from(0u8);
assert_eq!(zero, Number::Zero);
let one = Number::from(1u8);
assert_eq!(one, Number::NonZero(1_u8));
let two = Number::from(2u8);
assert_eq!(two, Number::NonZero(2_u8));
}
```
As this is naturally exhaustive, this is only supported for `FromPrimitive`, not also `TryFromPrimitive`.
Unsafely turning a primitive into an enum with from_unchecked
-------------------------------------------------------------
If you're really certain a conversion will succeed (and have not made use of `#[num_enum(default)]` or `#[num_enum(alternatives = [..])]`
for any of its variants), 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 your program.
```rust
use num_enum::UnsafeFromPrimitive;
#[derive(Debug, Eq, PartialEq, UnsafeFromPrimitive)]
#[repr(u8)]
enum Number {
Zero,
One,
}
fn main() {
assert_eq!(
unsafe { Number::from_unchecked(0_u8) },
Number::Zero,
);
assert_eq!(
unsafe { Number::from_unchecked(1_u8) },
Number::One,
);
}
unsafe fn undefined_behavior() {
let _ = Number::from_unchecked(2); // 2 is not a valid discriminant!
}
```
Optional features
-----------------
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.
License
-------
num_enum may be used under your choice of the BSD 3-clause, Apache 2, or MIT license.
|