-
Notifications
You must be signed in to change notification settings - Fork 43
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
#[non_exhaustive]
enum support
#94
Comments
Does the example code have an error? Of course it panics if you unwrap and don't have a variant for |
The example is with an non-exhaustive enum. I had hoped that, as it has both a defined representation in bits, and is marked non_exhaustive, i might be able to convert where i don't know what other states might exist in future. That is after all what |
Well you can do that, right? It does return a |
My view is that the conversion from u16 to As it is, this would be completely fine if the BitSpecifier were only ever consumed in a downstream crate - the non-exhaustive attribute already ensures that code contains a default case when pattern matching the enum. Making it safe when the enum is consumed within the same crate as the bitfield specifier is defined is harder, because the non-exhaustive attribute doesn't enforce the default case, and I'm unsure if there is a mechanism to do so. |
match ouid {
Ouid::SomeOrg => /* handle SomeOrg */
_ => /* handle non-defined Ouid */
} This works, since ouid is already type
|
One idea I had was to use some kind of conditional compilation to define a variant only on the defining crate, which is hidden for downstream crates. Then we could parse into such a variant and downstream crates could not match on it, only match on |
Looking at this more, I don't think there is any way, short of compiling to a seperate crate that gives the framework required to do this safely. I'm not that up on macro magic, but I doubt is sufficiently powerful to generate a new dependency crate at compile time? |
It could transmute. In this case, where the memory representation is known, and all variants are explicitly given, then I believe this would be safe. The problem remains that we cannot enforce non-exhaustive matching within the defining crate |
Actually, it won't transmute. match value {
1 => Ouid::SomeOrg
_ => core::mem::transmute(value)
} it will just return |
I recently hit a bug in packet parsing where I had a non-exhaustive enum, representing an Organization Identifier, which is represented using a non-exhaustive enum. This makes sense: its intractable to keep a full list of all possible Organizations, but the ones I care about I want to have enumerated.
However, this panics.
I think the correct behavior for
non_exhaustive
tagged Specifier is to not check the fieldsThe text was updated successfully, but these errors were encountered: