1
0
mirror of https://github.com/danog/strum.git synced 2024-12-02 17:38:15 +01:00
strum/strum_tests/tests/from_repr.rs
Andrew Burkett aeaa19ad86
EnumIndex implementation (#185)
* Add EnumConstIndex

* Get working with discriminants

* Remove unused/(unneeded?) features

* Rename to EnumIndex and index(..). Make const_index conditional

* Get repr(..) working

* Fix issue to support rust 1.32

* Switch from VARIANT# to {ENUM}_{VARIANT} for variant constant names

* Expose constants as part of implementation

* Add discriminant error messages. Cargo fmt my code

* Add rustversion to make compilation conditional on 1.46

* Handle expr discriminants

* Fix generics handling

* Make constants always available. No need to only expose them when const_index is defined

* Change to FromDiscriminant. Only output a single function

* Don't make constants accessible

* Make rustversion a dev dependency in strum-tests due to upstream change

* Cleanup doc tests for const

* Rename to FromRepr/from_repr
2021-11-06 09:30:09 -07:00

64 lines
1.5 KiB
Rust

use strum::FromRepr;
#[derive(Debug, FromRepr, PartialEq)]
#[repr(u8)]
enum Week {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday = 4 + 3,
Saturday = 8,
}
#[test]
fn simple_test() {
assert_eq!(Week::from_repr(0), Some(Week::Sunday));
assert_eq!(Week::from_repr(1), Some(Week::Monday));
assert_eq!(Week::from_repr(6), None);
assert_eq!(Week::from_repr(7), Some(Week::Friday));
assert_eq!(Week::from_repr(8), Some(Week::Saturday));
assert_eq!(Week::from_repr(9), None);
}
#[rustversion::since(1.46)]
#[test]
fn const_test() {
// This is to test that it works in a const fn
const fn from_repr(discriminant: u8) -> Option<Week> {
Week::from_repr(discriminant)
}
assert_eq!(from_repr(0), Some(Week::Sunday));
assert_eq!(from_repr(1), Some(Week::Monday));
assert_eq!(from_repr(6), None);
assert_eq!(from_repr(7), Some(Week::Friday));
assert_eq!(from_repr(8), Some(Week::Saturday));
assert_eq!(from_repr(9), None);
}
#[test]
fn crate_module_path_test() {
pub mod nested {
pub mod module {
pub use strum;
}
}
#[derive(Debug, FromRepr, PartialEq)]
#[strum(crate = "nested::module::strum")]
enum Week {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
}
assert_eq!(Week::from_repr(0), Some(Week::Sunday));
assert_eq!(Week::from_repr(6), Some(Week::Saturday));
assert_eq!(Week::from_repr(7), None);
}