Skip to content

Commit fff471a

Browse files
committed
feat: Iterators
Iterate over values and key-value zipped iterators.
1 parent 6953bdd commit fff471a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Debug and Eq are optional features. They are enabled by default.
2020

2121
## Usage
2222

23+
Please refer to the [documentation(https://docs.rs/enum-collections/latest/enum_collections/)] for a complete list of features and more in-depth documentation.
24+
2325
```rust
2426
use enum_collections::{EnumMap, Enumerated};
2527

enum-collections/src/enummap.rs

+44-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
ops::{Index, IndexMut},
77
};
88

9-
///
9+
/// Creates an EnumMap with user-provided values.
1010
/// ```
1111
/// use enum_collections::{em, Enumerated, EnumMap};
1212
/// #[derive(Enumerated)]
@@ -33,7 +33,7 @@ macro_rules! em {
3333

3434
}
3535

36-
///
36+
/// Initializes an EnumMap with default values for all variants not explicitly specified.
3737
/// ```
3838
/// use enum_collections::{em_default, Enumerated, EnumMap};
3939
/// #[derive(Enumerated)]
@@ -231,6 +231,48 @@ impl<K: Enumerated, V, const N: usize> EnumMap<K, V, N> {
231231
}
232232
}
233233

234+
/// Iterates over the EnumMap's key-value pairs.
235+
///
236+
/// ```
237+
/// use enum_collections::{EnumMap, Enumerated};
238+
/// #[derive(Enumerated, Debug)]
239+
/// pub enum Letter {
240+
/// A,
241+
/// B,
242+
/// }
243+
///
244+
/// let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
245+
/// for (_letter, value) in enum_map.iter_kv() {
246+
/// assert_eq!(42, *value);
247+
/// }
248+
///
249+
/// ```
250+
#[cfg(feature = "variants")]
251+
pub fn iter_kv(&self) -> std::iter::Zip<std::slice::Iter<'_, K>, std::slice::Iter<'_, V>> {
252+
K::VARIANTS.iter().zip(self.data.iter())
253+
}
254+
255+
/// Iterates over the EnumMap's values.
256+
///
257+
/// ```
258+
/// use enum_collections::{EnumMap, Enumerated};
259+
/// #[derive(Enumerated, Debug)]
260+
/// pub enum Letter {
261+
/// A,
262+
/// B,
263+
/// }
264+
///
265+
/// let enum_map = EnumMap::<Letter, i32, { Letter::SIZE }>::new(|| 42);
266+
/// for value in enum_map.iter() {
267+
/// assert_eq!(42, *value);
268+
/// }
269+
///
270+
/// ```
271+
#[cfg(feature = "variants")]
272+
pub fn iter(&self) -> std::slice::Iter<'_, V> {
273+
self.data.iter()
274+
}
275+
234276
/// Creates a new EnumMap where value of each variant is produced by the provided function.
235277
/// The function receives the enum variant being initialized for inspection.
236278
///

0 commit comments

Comments
 (0)