Re:API design note: Beware of adding an "Other" enum value
by sophacles on Monday March 03, 2025 @04:56PM
(#43247323)
Looking at my code that works on this stuff - the above is just wrong. I was looking at my failed experimental branch not the actual code that does this. The above is a fun way to introduce all sorts of UB.
Apologies for my pre-coffee brainfarts.
Re:API design note: Beware of adding an "Other" enum value
by codetrotter on Monday March 03, 2025 @08:59PM
(#43249310)
How does the working code look?
This allows you to do something like:
This lets you have a simple fast parsing of types without needing a bunch of logic - particularly in the ref example. Someone else sent you data over the wire and is using a vendor defined value, or a newer version of the protocol that defines Foo::D? No big deal, you can igore it or error, or whatever else is appropriate for your case.If you want to define Reserved and Vendor as enum attributes, now you have to have logic that runs all the time - and if you want to preserve the original value for error messages, logs, etc - you can't Repr(u8) and take up more memory, have to do copies, etc.
You also need logic to convert back to a u8 now too.It's not strictly necessary, but it certainly makes some things far more ergonomic.