@@ -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.
11661176pub 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+
11801238fn hash_with < K , S > ( key : & K , build_hasher : & S ) -> HashValue
11811239where
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}
0 commit comments