Skip to content

Commit

Permalink
Replace match statement with if
Browse files Browse the repository at this point in the history
rustc doesn’t yet realize that enum variants without explicit
discriminators are constant enough to be used as patterns in a match
statement (rust-lang/rust#23898).

This happens to be what #[derive(FromPrimitive)] does, anyway.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Apr 8, 2015
1 parent b8ecbb2 commit 173a5a8
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ pub use num::FromPrimitive;
#[macro_export]
macro_rules! enum_from_primitive_impl_ty {
($meth:ident, $ty:ty, $name:ident, $( $variant:ident ),*) => {
#[allow(non_upper_case_globals)]
#[allow(non_upper_case_globals, unused)]
fn $meth(n: $ty) -> $crate::Option<Self> {
$( const $variant: $ty = $name::$variant as $ty; )*
match n {
$( $variant => $crate::Option::Some($name::$variant), )*
_ => $crate::Option::None,
$( if n == $name::$variant as $ty {
$crate::Option::Some($name::$variant)
} else )* {
$crate::Option::None
}
}
}
Expand Down

0 comments on commit 173a5a8

Please sign in to comment.