1
0
mirror of https://github.com/danog/strum.git synced 2024-12-02 09:27:57 +01:00

Fix building on rust < 1.34

This commit is contained in:
Daniil Gentili 2022-04-19 21:06:39 +02:00
parent 9318da3d3e
commit cf34a64ee9
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 12 additions and 8 deletions

View File

@ -381,7 +381,8 @@ pub fn enum_iter(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
toks.into() 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 /// 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 /// `from_repr(discriminant: usize) -> Option<YourEnum>` as a standalone function on the enum. For

View File

@ -11,6 +11,7 @@ clap = "=2.33.0"
enum_variant_type = "=0.2.0" enum_variant_type = "=0.2.0"
structopt = "0.2.18" structopt = "0.2.18"
bitflags = "=1.2" bitflags = "=1.2"
if_rust_version = "1.0"
[dev-dependencies] [dev-dependencies]
rustversion = "1.0" rustversion = "1.0"

View File

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