diff --git a/strum_macros/src/lib.rs b/strum_macros/src/lib.rs index 32f9a78..a6ff225 100644 --- a/strum_macros/src/lib.rs +++ b/strum_macros/src/lib.rs @@ -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 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` as a standalone function on the enum. For diff --git a/strum_tests/Cargo.toml b/strum_tests/Cargo.toml index 796a069..cf15adf 100644 --- a/strum_tests/Cargo.toml +++ b/strum_tests/Cargo.toml @@ -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" diff --git a/strum_tests/tests/from_repr.rs b/strum_tests/tests/from_repr.rs index 76617a7..2b46042 100644 --- a/strum_tests/tests/from_repr.rs +++ b/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)] @@ -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)); + }} }; }