repr-with-fallback
Automatically generate [From
] and [Into
] impls for enum
s with custom discriminant values
and a fallback variant.
```rs use reprwithfallback::reprwithfallback;
reprwithfallback! { /// A DNSSEC algorithm. #[derive(Debug, PartialEq)] pub enum Algorithm { /// ... RSASHA256 = 8, RSASHA512 = 10, ECDSAP256SHA256 = 13, ECDSAP384SHA384 = 14, ED25519 = 15, Unassigned(u8), } }
asserteq!(u8::from(Algorithm::ED25519), 15); asserteq!(Algorithm::from(15), Algorithm::ED25519);
asserteq!(u8::from(Algorithm::Unassigned(17)), 17); asserteq!(Algorithm::from(17), Algorithm::Unassigned(17)); ```
There are two restrictions imposed on the enum
:
The repr type does not need to be numerical:
```rs reprwithfallback! { pub enum Strings { Foo = "static", Bar = "string", Baz = "slices", Spam = "work", Eggs = "too", Unknown(&'static str), } }
let s: &'static str = Strings::Foo.into(); assert_eq!(s, "static"); ```