You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`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.
31
29
32
-
Attempting to turn a primitive into an enum with try_from
33
-
----------------------------------------------
30
+
## Attempting to turn a primitive into an enum with try_from
34
31
35
32
```rust
36
33
usenum_enum::TryFromPrimitive;
@@ -55,8 +52,7 @@ fn main() {
55
52
}
56
53
```
57
54
58
-
Variant alternatives
59
-
---------------
55
+
### Variant alternatives
60
56
61
57
Sometimes a single enum variant might be representable by multiple numeric values.
62
58
@@ -122,43 +118,34 @@ fn main() {
122
118
}
123
119
```
124
120
125
-
Default variant
126
-
---------------
121
+
### Custom error types
127
122
128
-
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.
129
-
130
-
The `#[num_enum(default)]` attribute (or the stdlib `#[default]` attribute) allows you to mark variant as the default.
123
+
`TryFromPrimitive` by default will use `num_enum::TryFromPrimitiveError` as its `Error` type.
131
124
132
-
(The behavior of `IntoPrimitive` is unaffected by this attribute, it will always return the canonical value.)
125
+
If you want to use a different type, you can use an annotation for this:
## Safely turning a primitive into an exhaustive enum with from_primitive
162
149
163
150
If your enum has all possible primitive values covered, you can derive `FromPrimitive` for it (which auto-implement stdlib's `From`):
164
151
@@ -191,8 +178,41 @@ fn main() {
191
178
}
192
179
```
193
180
194
-
Catch-all variant
195
-
-----------------
181
+
### Default variant
182
+
183
+
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.
184
+
185
+
The `#[num_enum(default)]` attribute (or the stdlib `#[default]` attribute) allows you to mark variant as the default.
186
+
187
+
(The behavior of `IntoPrimitive` is unaffected by this attribute, it will always return the canonical value.)
188
+
189
+
```rust
190
+
usenum_enum::FromPrimitive;
191
+
usestd::convert::TryFrom;
192
+
193
+
#[derive(Debug, Eq, PartialEq, FromPrimitive)]
194
+
#[repr(u8)]
195
+
enumNumber {
196
+
Zero=0,
197
+
#[num_enum(default)]
198
+
NonZero=1,
199
+
}
200
+
201
+
fnmain() {
202
+
letzero=Number::from(0u8);
203
+
assert_eq!(zero, Number::Zero);
204
+
205
+
letone=Number::from(1u8);
206
+
assert_eq!(one, Number::NonZero);
207
+
208
+
lettwo=Number::from(2u8);
209
+
assert_eq!(two, Number::NonZero);
210
+
}
211
+
```
212
+
213
+
Only `FromPrimitive` pays attention to `default` attributes, `TryFromPrimitive` ignores them.
214
+
215
+
### Catch-all variant
196
216
197
217
Sometimes it is desirable to have an `Other` variant which holds the otherwise un-matched value as a field.
198
218
@@ -224,34 +244,13 @@ fn main() {
224
244
225
245
As this is naturally exhaustive, this is only supported for `FromPrimitive`, not also `TryFromPrimitive`.
226
246
227
-
Unsafely turning a primitive into an enum with unchecked_transmute_from
0 commit comments