Skip to content

Commit 8c044fb

Browse files
committed
feat: Enumerated implementation for bool
1 parent f93dacb commit 8c044fb

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

enum-collections/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ debug = ["variants"] # Array of all variants must be available at runtime to gen
2020
eq = []
2121
# Generates a static array of all variants for the EnumMap. This is required for the `Debug` implementation.
2222
variants = ["enum-collections-macros/variants"]
23+
# Implementations of the `Enumerated` trait for common data types.
24+
ext=[]
2325

2426
[dependencies]
2527
enum-collections-macros = { path = "../enum-collections-macros", version = "1.0.0" }

enum-collections/src/ext.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

enum-collections/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
//!
1818
mod enumerated;
1919
mod enummap;
20+
#[cfg(feature = "ext")]
21+
mod ext;
2022

2123
pub use crate::enumerated::Enumerated;
2224
pub use crate::enummap::EnumMap;

0 commit comments

Comments
 (0)