File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed
Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ debug = ["variants"] # Array of all variants must be available at runtime to gen
2020eq = []
2121# Generates a static array of all variants for the EnumMap. This is required for the `Debug` implementation.
2222variants = [" enum-collections-macros/variants" ]
23+ # Implementations of the `Enumerated` trait for common data types.
24+ ext =[]
2325
2426[dependencies ]
2527enum-collections-macros = { path = " ../enum-collections-macros" , version = " 1.0.0" }
Original file line number Diff line number Diff line change 1+ //! Implementations of the [Enumerated] trait for common data types.
2+
3+ use crate :: Enumerated ;
4+
5+ impl Enumerated for bool {
6+ const SIZE : usize = 2 ;
7+ const VARIANTS : & ' static [ Self ] = & [ false , true ] ;
8+
9+ fn position ( self ) -> usize {
10+ self as usize
11+ }
12+ }
13+
14+ #[ cfg( test) ]
15+ mod tests {
16+ use crate :: { em_default, EnumMap , Enumerated } ;
17+
18+ #[ test]
19+ fn test_bool ( ) {
20+ assert_eq ! ( 0 , false . position( ) ) ;
21+ assert_eq ! ( 1 , true . position( ) ) ;
22+ assert_eq ! ( 2 , bool :: SIZE ) ;
23+
24+ let map = EnumMap :: < bool , i32 , { bool:: SIZE } > :: new_inspect ( |variant| match variant {
25+ false => 42 ,
26+ true => 24 ,
27+ } ) ;
28+
29+ assert_eq ! ( 42 , map[ false ] ) ;
30+ assert_eq ! ( 24 , map[ true ] ) ;
31+
32+ let map = em_default ! ( bool , i32 , ) ;
33+ for variant in bool:: VARIANTS {
34+ assert_eq ! ( 0 , map[ * variant] ) ;
35+ }
36+ }
37+ }
Original file line number Diff line number Diff line change 1717//!
1818mod enumerated;
1919mod enummap;
20+ #[ cfg( feature = "ext" ) ]
21+ mod ext;
2022
2123pub use crate :: enumerated:: Enumerated ;
2224pub use crate :: enummap:: EnumMap ;
You can’t perform that action at this time.
0 commit comments