Skip to content

Commit 217456a

Browse files
bors[bot]jeandudey
andauthored
Merge #338
338: export all IndexMap iterators r=korken89 a=jeandudey Exports all the iterator types of `IndexMap`. I wonder if it'd make sense to have a module structure such as the one in `alloc::collections`, e.g.: `heapless::indexmap` and re-export `heapless::indexmap::IndexMap` on the crate root, to have all the iterator types inside the `indexmap` module but still public. Co-authored-by: Jean-Pierre De Jesus DIAZ <[email protected]>
2 parents cd23629 + 9fe93fd commit 217456a

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2727
subset of ARM targets. See the module level documentation of the `pool` module for details
2828
- relax trait requirements on `IndexMap` and `IndexSet`.
2929
- export `IndexSet` and `IndexMap` iterator types.
30+
- [breaking-change] export `IndexMapKeys`, `IndexMapValues` and
31+
`IndexMapValuesMut` iterator types.
3032

3133
- [breaking-change] this crate now depends on `atomic-polyfill` v1.0.1, meaning that targets that
3234
require a polyfill need a `critical-section` **v1.x.x** implementation.

src/indexmap.rs

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,10 @@ impl<K, V, S, const N: usize> IndexMap<K, V, S, N> {
567567
/// println!("{}", key);
568568
/// }
569569
/// ```
570-
pub fn keys(&self) -> impl Iterator<Item = &K> {
571-
self.core.entries.iter().map(|bucket| &bucket.key)
570+
pub fn keys(&self) -> Keys<'_, K, V> {
571+
Keys {
572+
iter: self.core.entries.iter(),
573+
}
572574
}
573575

574576
/// Return an iterator over the values of the map, in insertion order
@@ -585,8 +587,10 @@ impl<K, V, S, const N: usize> IndexMap<K, V, S, N> {
585587
/// println!("{}", val);
586588
/// }
587589
/// ```
588-
pub fn values(&self) -> impl Iterator<Item = &V> {
589-
self.core.entries.iter().map(|bucket| &bucket.value)
590+
pub fn values(&self) -> Values<'_, K, V> {
591+
Values {
592+
iter: self.core.entries.iter(),
593+
}
590594
}
591595

592596
/// Return an iterator over mutable references to the the values of the map, in insertion order
@@ -607,8 +611,10 @@ impl<K, V, S, const N: usize> IndexMap<K, V, S, N> {
607611
/// println!("{}", val);
608612
/// }
609613
/// ```
610-
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
611-
self.core.entries.iter_mut().map(|bucket| &mut bucket.value)
614+
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
615+
ValuesMut {
616+
iter: self.core.entries.iter_mut(),
617+
}
612618
}
613619

614620
/// Return an iterator over the key-value pairs of the map, in insertion order
@@ -1163,6 +1169,10 @@ impl<'a, K, V> Clone for Iter<'a, K, V> {
11631169
}
11641170
}
11651171

1172+
/// A mutable iterator over the items of a [`IndexMap`].
1173+
///
1174+
/// This `struct` is created by the [`iter_mut`](IndexMap::iter_mut) method on [`IndexMap`]. See its
1175+
/// documentation for more.
11661176
pub struct IterMut<'a, K, V> {
11671177
iter: slice::IterMut<'a, Bucket<K, V>>,
11681178
}
@@ -1177,6 +1187,54 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
11771187
}
11781188
}
11791189

1190+
/// An iterator over the keys of a [`IndexMap`].
1191+
///
1192+
/// This `struct` is created by the [`keys`](IndexMap::keys) method on [`IndexMap`]. See its
1193+
/// documentation for more.
1194+
pub struct Keys<'a, K, V> {
1195+
iter: slice::Iter<'a, Bucket<K, V>>,
1196+
}
1197+
1198+
impl<'a, K, V> Iterator for Keys<'a, K, V> {
1199+
type Item = &'a K;
1200+
1201+
fn next(&mut self) -> Option<Self::Item> {
1202+
self.iter.next().map(|bucket| &bucket.key)
1203+
}
1204+
}
1205+
1206+
/// An iterator over the values of a [`IndexMap`].
1207+
///
1208+
/// This `struct` is created by the [`values`](IndexMap::values) method on [`IndexMap`]. See its
1209+
/// documentation for more.
1210+
pub struct Values<'a, K, V> {
1211+
iter: slice::Iter<'a, Bucket<K, V>>,
1212+
}
1213+
1214+
impl<'a, K, V> Iterator for Values<'a, K, V> {
1215+
type Item = &'a V;
1216+
1217+
fn next(&mut self) -> Option<Self::Item> {
1218+
self.iter.next().map(|bucket| &bucket.value)
1219+
}
1220+
}
1221+
1222+
/// A mutable iterator over the values of a [`IndexMap`].
1223+
///
1224+
/// This `struct` is created by the [`values_mut`](IndexMap::values_mut) method on [`IndexMap`]. See its
1225+
/// documentation for more.
1226+
pub struct ValuesMut<'a, K, V> {
1227+
iter: slice::IterMut<'a, Bucket<K, V>>,
1228+
}
1229+
1230+
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
1231+
type Item = &'a mut V;
1232+
1233+
fn next(&mut self) -> Option<Self::Item> {
1234+
self.iter.next().map(|bucket| &mut bucket.value)
1235+
}
1236+
}
1237+
11801238
fn hash_with<K, S>(key: &K, build_hasher: &S) -> HashValue
11811239
where
11821240
K: ?Sized + Hash,
@@ -1468,4 +1526,32 @@ mod tests {
14681526
assert_eq!(Some((&0, &1)), map.first());
14691527
assert_eq!(Some((&1, &2)), map.last());
14701528
}
1529+
1530+
#[test]
1531+
fn keys_iter() {
1532+
let map = almost_filled_map();
1533+
for (&key, i) in map.keys().zip(1..MAP_SLOTS) {
1534+
assert_eq!(key, i);
1535+
}
1536+
}
1537+
1538+
#[test]
1539+
fn values_iter() {
1540+
let map = almost_filled_map();
1541+
for (&value, i) in map.values().zip(1..MAP_SLOTS) {
1542+
assert_eq!(value, i);
1543+
}
1544+
}
1545+
1546+
#[test]
1547+
fn values_mut_iter() {
1548+
let mut map = almost_filled_map();
1549+
for value in map.values_mut() {
1550+
*value += 1;
1551+
}
1552+
1553+
for (&value, i) in map.values().zip(1..MAP_SLOTS) {
1554+
assert_eq!(value, i + 1);
1555+
}
1556+
}
14711557
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ pub use binary_heap::BinaryHeap;
8383
pub use deque::Deque;
8484
pub use histbuf::{HistoryBuffer, OldestOrdered};
8585
pub use indexmap::{
86-
Bucket, Entry, FnvIndexMap, IndexMap, Iter as IndexMapIter, OccupiedEntry, Pos, VacantEntry,
86+
Bucket, Entry, FnvIndexMap, IndexMap, Iter as IndexMapIter, IterMut as IndexMapIterMut,
87+
Keys as IndexMapKeys, OccupiedEntry, Pos, VacantEntry, Values as IndexMapValues,
88+
ValuesMut as IndexMapValuesMut,
8789
};
8890
pub use indexset::{FnvIndexSet, IndexSet, Iter as IndexSetIter};
8991
pub use linear_map::LinearMap;

0 commit comments

Comments
 (0)