3 ms·
You can do this by just implementing From<u8> and Into<u8> for your type - using the enum representation only for assignment or pattern matching. The more prin
by zozbot234 3y ago
You can do this by just implementing From<u8> and Into<u8> for your type - using the enum representation only for assignment or pattern matching. The more principled solution AIUI would be to have "patterns" as a first-class citizen within the language.
Storing the tag 'out of band' is something you can only do as part of some larger object, in which case you can similarly have getters and setters that take or return enums and do the appropriate conversion.
- brigadier132 3y ago> From<u8> and Into<u8> for your type It gets verbose fast when you are talking about all combinations of variants of an enum.
- zozbot234 3y agoThe point is that you only have to do it once, when defining the object. Everything else then happens via the From and Into implementations, which the compiler will generally be smart enough to inline. So it'll be just as efficient as working on the underlying u8.
- brigadier132 3y agoYou have to do it once for each subset of variants your state machine requires. Which if you enumerate them all is 2^N from implementations.
- hurril 3y agoThen what does this mean? let x: u8 = E::Rest(0).into(); let y: E = x.into();
- duped 3y agoThat's not equivalent. Notably if you have a `&[u8]` you can't transmute it to `&[E]`. It's also noisy when you have the enum as part of a larger struct, and can make encoding/decoding very verbose.