-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default value for enums #1429
Comments
Thank you for the feature request! The let bytes = &[42u8];
let ot: &OperationType = OperationType::from_bytes(bytes) Both That said, if your complaint is that defining 256 variants is irritating, I can only wholeheartedly agree. @joshlf perhaps we could provide an attribute macro to generate these excess variants. |
Understood and thanks for the thoughtful reply. I suppose in general this is the issue with SerDe libraries ... there is an impedance mismatch between the semantics of the language into which the bytes are deserialized and the bytes themselves (and the attendant "wire" specification). Instead of expecting the language to match perfectly to the serialized format e.g.: #[repr(u8)]
pub struct enum OperationType {
Foo = 0,
Bar = 1,
Baz = 2,
}
let bytes = &[1u8];
let ot = &OperationType = OperationType::from_bytes(bytes); We could expect that the language representation includes some method to detect a mismatch between the library and the binary representation. For example, the Rust SerDe library expects you to call let bytes = &[3u8];
let ot: Enum<OperationType> = OperationType::from_bytes(bytes);
match ot.get() {
Some(e) => {/*deserializes*/},
None => {/*fails*/}
}
let raw = ot.raw();
let raw_mut = ot.raw_mut(); // Could return mutable slice containing just the value ... These method signatures are sketches for discussion. Tastes vary on specific method names and signatures :) |
Project
It's not public but I am working on a high-throughput IO block device subsystem.
Use Case
I want to use enums for an operation log where the enum represents the operation type e.g. Put or Delete.
Current State
If I do
... I get an error that the enum must have all 256 variants.
Desired State
It sure would be nice if we could specify a default value somewhat like:
This is similar to how the serde handles this issue.
The text was updated successfully, but these errors were encountered: