Zhi Enum
Easily create casting traits with unknown field support.
Usage
- Add
EnumConvertorEnumTryConvertinto thederiveattribute.
use ;
- Then, you can use
.from/.intoconvert functions
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
use TryFrom;
assert_eq!;
Handle unknown discriminant
-
If you have defined a variant with
#[zhi_enum(unknown)]attribute in your enum like this :Whatever,which the
repr typeis the represented type as you define use#[repr(...)].Then, when you convert a repr type into enum type, an unknown discriminant will be converted to this variant with its unknown value.
-
If you have not defined, the
.try_into()will returnsErr(UnknownVariantError{}), and the.into()will callpanic!.
FAQ
-
Error E0658
If you get this error:
error[E0658]: custom discriminant values are not allowed in enums with tuple or struct variants --> <source>:2:9It means that your rustc version is lower than
1.56.You can just upgrade your rustc. Or if you don't want upgrade, you have to add
#![feature(arbitrary_enum_discriminant)]to your project.
License
BSD-3-Clause