-
Notifications
You must be signed in to change notification settings - Fork 13k
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
C-style enum doesn't work in match arm #44266
Comments
In fact, this has little to do with C-style enums per se. Instead, it's casts which aren't allowed in match arms: see eg #11791. That's... less than ideal. |
From that issue, the workaround seems to be:
That's doable but pretty annoying and unergonomic. |
I think the following is the more widely used workaround for that: match 0u8 {
x if x == Thing::Foo as u8 => { ... }
x if x == Thing::Bar as u8 => { ... }
_ => println!("..."),
} (Which obviously is even worse since you easily can get overlapping variants and lose |
Better workaround: #[macro_use] extern crate enum_primitive_derive;
extern crate num_traits;
use num_traits::FromPrimitive;
#[derive(Primitive)]
enum Thing {
Foo = 0,
Bar = 1,
}
match Thing::from_u8(0) {
Some(Thing::Foo) => ...,
Some(Thing::Bar) => ....,
None => panic!("out of range"),
} |
I think this explicitly SHOULD NOT work. In particular, I think we should be working towards the deprecation of the |
This is a feature request that sounds like it'll be a heavily discussed RFC. You should close it here and open an issue on the RFC repo or the internals forum |
Yup, agreed with @oli-obk . Please pursue this through the usual RFC-ish channels, thank you! |
I expected this to work, but it doesn't:
with:
Putting the cast inside extra parens doesn't help.
My use case is having an enum whose ordinals match an external protocol definition, then matching against the value read off the wire to convert into a member of the enum. I would expect this to be super common.
Previous history:
The text was updated successfully, but these errors were encountered: