Sometimes in code I use repr() on C-style enums:
#[repr(u8)]
enum MyEnum { A = b'A', B = b'B', C = b'C' }
And sometimes in such code I need to convert the base type (here u8) from and to the enum (here MyEnum).
You can convert MyEnum => u8 with a simple “as” cast:
let e1 = MyEnum::B;
let x = e1 as u8;
But the wise person knows that in Rust code the number of “as” should be minimized, because they are bug-prone.
And std::mem::discriminant
is not meant to solve this problem.
One way to solve this problem is to support the base type from():
let x = u8::from(e1);
A possibly better but less standard solution is to give enums a conversion method:
let x = e1.as_base_value();
assert_eq!(x, b'B');
A problem is that such method should be available only for C-like enums.