Skip to content

Commit

Permalink
Fix building on rust < 1.34
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Apr 19, 2022
1 parent 9318da3 commit cf34a64
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion strum_macros/src/lib.rs
Expand Up @@ -381,7 +381,8 @@ pub fn enum_iter(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
toks.into()
}

/// Add a function to enum that allows accessing variants by its discriminant
/// Add a function to enum that allows accessing variants by its discriminant.
/// On Rust 1.34 and above, std::convert::TryFrom<TDiscriminant> will be derived as well.
///
/// This macro adds a standalone function to obtain an enum variant by its discriminant. The macro adds
/// `from_repr(discriminant: usize) -> Option<YourEnum>` as a standalone function on the enum. For
Expand Down
1 change: 1 addition & 0 deletions strum_tests/Cargo.toml
Expand Up @@ -11,6 +11,7 @@ clap = "=2.33.0"
enum_variant_type = "=0.2.0"
structopt = "0.2.18"
bitflags = "=1.2"
if_rust_version = "1.0"

[dev-dependencies]
rustversion = "1.0"
16 changes: 9 additions & 7 deletions strum_tests/tests/from_repr.rs
@@ -1,6 +1,4 @@
use core::convert::TryFrom;
use core::convert::TryInto;

use if_rust_version::if_rust_version;
use strum::FromRepr;

#[derive(Debug, FromRepr, PartialEq)]
Expand All @@ -18,13 +16,17 @@ enum Week {
macro_rules! assert_eq_repr {
( $type:ident::from_repr($number:literal), Some($enum:expr) ) => {
assert_eq!($type::from_repr($number), Some($enum));
assert_eq!(TryInto::<$type>::try_into($number), Ok($enum));
assert_eq!(<$type as TryFrom<_>>::try_from($number), Ok($enum));
if_rust_version! { >= 1.34 {
assert_eq!(core::convert::TryInto::<$type>::try_into($number), Ok($enum));
assert_eq!(<$type as core::convert::TryFrom<_>>::try_from($number), Ok($enum));
}}
};
( $type:ident::from_repr($number:literal), None ) => {
assert_eq!($type::from_repr($number), None);
assert_eq!(TryInto::<$type>::try_into($number), Err(::strum::ParseError::VariantNotFound));
assert_eq!(<$type as TryFrom<_>>::try_from($number), Err(::strum::ParseError::VariantNotFound));
if_rust_version! { >= 1.34 {
assert_eq!(core::convert::TryInto::<$type>::try_into($number), Err(::strum::ParseError::VariantNotFound));
assert_eq!(<$type as core::convert::TryFrom<_>>::try_from($number), Err(::strum::ParseError::VariantNotFound));
}}
};
}

Expand Down

0 comments on commit cf34a64

Please sign in to comment.