Skip to content

Commit 173a5a8

Browse files
committed
Replace match statement with if
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 <andersk@mit.edu>
1 parent b8ecbb2 commit 173a5a8

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Diff for: src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ pub use num::FromPrimitive;
6464
#[macro_export]
6565
macro_rules! enum_from_primitive_impl_ty {
6666
($meth:ident, $ty:ty, $name:ident, $( $variant:ident ),*) => {
67-
#[allow(non_upper_case_globals)]
67+
#[allow(non_upper_case_globals, unused)]
6868
fn $meth(n: $ty) -> $crate::Option<Self> {
69-
$( const $variant: $ty = $name::$variant as $ty; )*
70-
match n {
71-
$( $variant => $crate::Option::Some($name::$variant), )*
72-
_ => $crate::Option::None,
69+
$( if n == $name::$variant as $ty {
70+
$crate::Option::Some($name::$variant)
71+
} else )* {
72+
$crate::Option::None
7373
}
7474
}
7575
}

0 commit comments

Comments
 (0)