@@ -567,8 +567,10 @@ impl<K, V, S, const N: usize> IndexMap<K, V, S, N> {
567
567
/// println!("{}", key);
568
568
/// }
569
569
/// ```
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
+ }
572
574
}
573
575
574
576
/// 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> {
585
587
/// println!("{}", val);
586
588
/// }
587
589
/// ```
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
+ }
590
594
}
591
595
592
596
/// 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> {
607
611
/// println!("{}", val);
608
612
/// }
609
613
/// ```
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
+ }
612
618
}
613
619
614
620
/// 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> {
1163
1169
}
1164
1170
}
1165
1171
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.
1166
1176
pub struct IterMut < ' a , K , V > {
1167
1177
iter : slice:: IterMut < ' a , Bucket < K , V > > ,
1168
1178
}
@@ -1177,6 +1187,54 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
1177
1187
}
1178
1188
}
1179
1189
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
+
1180
1238
fn hash_with < K , S > ( key : & K , build_hasher : & S ) -> HashValue
1181
1239
where
1182
1240
K : ?Sized + Hash ,
@@ -1468,4 +1526,32 @@ mod tests {
1468
1526
assert_eq ! ( Some ( ( & 0 , & 1 ) ) , map. first( ) ) ;
1469
1527
assert_eq ! ( Some ( ( & 1 , & 2 ) ) , map. last( ) ) ;
1470
1528
}
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
+ }
1471
1557
}
0 commit comments