File tree 3 files changed +41
-0
lines changed
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
20
20
eq = []
21
21
# Generates a static array of all variants for the EnumMap. This is required for the `Debug` implementation.
22
22
variants = [" enum-collections-macros/variants" ]
23
+ # Implementations of the `Enumerated` trait for common data types.
24
+ ext =[]
23
25
24
26
[dependencies ]
25
27
enum-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 17
17
//!
18
18
mod enumerated;
19
19
mod enummap;
20
+ #[ cfg( feature = "ext" ) ]
21
+ mod ext;
20
22
21
23
pub use crate :: enumerated:: Enumerated ;
22
24
pub use crate :: enummap:: EnumMap ;
You can’t perform that action at this time.
0 commit comments