@@ -580,6 +580,27 @@ impl<'a, K, V> Entry<'a, K, V> {
580
580
}
581
581
}
582
582
583
+ impl < ' a , K : ' a + fmt:: Debug , V : ' a + fmt:: Debug > fmt:: Debug for Entry < ' a , K , V > {
584
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
585
+ match * self {
586
+ Entry :: Vacant ( ref v) => {
587
+ f. debug_tuple ( "Entry" )
588
+ . field ( v)
589
+ . finish ( )
590
+ }
591
+ Entry :: Occupied ( ref o) => {
592
+ f. debug_tuple ( "Entry" )
593
+ . field ( o)
594
+ . finish ( )
595
+ }
596
+ }
597
+ }
598
+ }
599
+
600
+ /// A view into an occupied entry in a `IndexMap`.
601
+ /// It is part of the [`Entry`] enum.
602
+ ///
603
+ /// [`Entry`]: enum.Entry.html
583
604
pub struct OccupiedEntry < ' a , K : ' a , V : ' a > {
584
605
map : & ' a mut OrderMapCore < K , V > ,
585
606
key : K ,
@@ -625,7 +646,19 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
625
646
}
626
647
}
627
648
649
+ impl < ' a , K : ' a + fmt:: Debug , V : ' a + fmt:: Debug > fmt:: Debug for OccupiedEntry < ' a , K , V > {
650
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
651
+ f. debug_struct ( "OccupiedEntry" )
652
+ . field ( "key" , self . key ( ) )
653
+ . field ( "value" , self . get ( ) )
654
+ . finish ( )
655
+ }
656
+ }
628
657
658
+ /// A view into a vacant entry in a `IndexMap`.
659
+ /// It is part of the [`Entry`] enum.
660
+ ///
661
+ /// [`Entry`]: enum.Entry.html
629
662
pub struct VacantEntry < ' a , K : ' a , V : ' a > {
630
663
map : & ' a mut OrderMapCore < K , V > ,
631
664
key : K ,
@@ -657,6 +690,14 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
657
690
}
658
691
}
659
692
693
+ impl < ' a , K : ' a + fmt:: Debug , V : ' a > fmt:: Debug for VacantEntry < ' a , K , V > {
694
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
695
+ f. debug_tuple ( "VacantEntry" )
696
+ . field ( self . key ( ) )
697
+ . finish ( )
698
+ }
699
+ }
700
+
660
701
impl < K , V , S > IndexMap < K , V , S >
661
702
where K : Hash + Eq ,
662
703
S : BuildHasher ,
@@ -1455,6 +1496,13 @@ use std::slice::Iter as SliceIter;
1455
1496
use std:: slice:: IterMut as SliceIterMut ;
1456
1497
use std:: vec:: IntoIter as VecIntoIter ;
1457
1498
1499
+ /// An iterator over the keys of a `IndexMap`.
1500
+ ///
1501
+ /// This `struct` is created by the [`keys`] method on [`IndexMap`]. See its
1502
+ /// documentation for more.
1503
+ ///
1504
+ /// [`keys`]: struct.IndexMap.html#method.keys
1505
+ /// [`IndexMap`]: struct.IndexMap.html
1458
1506
pub struct Keys < ' a , K : ' a , V : ' a > {
1459
1507
pub ( crate ) iter : SliceIter < ' a , Bucket < K , V > > ,
1460
1508
}
@@ -1477,6 +1525,28 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
1477
1525
}
1478
1526
}
1479
1527
1528
+ // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
1529
+ impl < ' a , K , V > Clone for Keys < ' a , K , V > {
1530
+ fn clone ( & self ) -> Keys < ' a , K , V > {
1531
+ Keys { iter : self . iter . clone ( ) }
1532
+ }
1533
+ }
1534
+
1535
+ impl < ' a , K : fmt:: Debug , V > fmt:: Debug for Keys < ' a , K , V > {
1536
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1537
+ f. debug_list ( )
1538
+ . entries ( self . clone ( ) )
1539
+ . finish ( )
1540
+ }
1541
+ }
1542
+
1543
+ /// An iterator over the values of a `IndexMap`.
1544
+ ///
1545
+ /// This `struct` is created by the [`values`] method on [`IndexMap`]. See its
1546
+ /// documentation for more.
1547
+ ///
1548
+ /// [`values`]: struct.IndexMap.html#method.values
1549
+ /// [`IndexMap`]: struct.IndexMap.html
1480
1550
pub struct Values < ' a , K : ' a , V : ' a > {
1481
1551
iter : SliceIter < ' a , Bucket < K , V > > ,
1482
1552
}
@@ -1499,6 +1569,28 @@ impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
1499
1569
}
1500
1570
}
1501
1571
1572
+ // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
1573
+ impl < ' a , K , V > Clone for Values < ' a , K , V > {
1574
+ fn clone ( & self ) -> Values < ' a , K , V > {
1575
+ Values { iter : self . iter . clone ( ) }
1576
+ }
1577
+ }
1578
+
1579
+ impl < ' a , K , V : fmt:: Debug > fmt:: Debug for Values < ' a , K , V > {
1580
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1581
+ f. debug_list ( )
1582
+ . entries ( self . clone ( ) )
1583
+ . finish ( )
1584
+ }
1585
+ }
1586
+
1587
+ /// A mutable iterator over the values of a `IndexMap`.
1588
+ ///
1589
+ /// This `struct` is created by the [`values_mut`] method on [`IndexMap`]. See its
1590
+ /// documentation for more.
1591
+ ///
1592
+ /// [`values_mut`]: struct.IndexMap.html#method.values_mut
1593
+ /// [`IndexMap`]: struct.IndexMap.html
1502
1594
pub struct ValuesMut < ' a , K : ' a , V : ' a > {
1503
1595
iter : SliceIterMut < ' a , Bucket < K , V > > ,
1504
1596
}
@@ -1521,6 +1613,13 @@ impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> {
1521
1613
}
1522
1614
}
1523
1615
1616
+ /// An iterator over the entries of a `IndexMap`.
1617
+ ///
1618
+ /// This `struct` is created by the [`iter`] method on [`IndexMap`]. See its
1619
+ /// documentation for more.
1620
+ ///
1621
+ /// [`iter`]: struct.IndexMap.html#method.iter
1622
+ /// [`IndexMap`]: struct.IndexMap.html
1524
1623
pub struct Iter < ' a , K : ' a , V : ' a > {
1525
1624
iter : SliceIter < ' a , Bucket < K , V > > ,
1526
1625
}
@@ -1543,6 +1642,28 @@ impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
1543
1642
}
1544
1643
}
1545
1644
1645
+ // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
1646
+ impl < ' a , K , V > Clone for Iter < ' a , K , V > {
1647
+ fn clone ( & self ) -> Iter < ' a , K , V > {
1648
+ Iter { iter : self . iter . clone ( ) }
1649
+ }
1650
+ }
1651
+
1652
+ impl < ' a , K : fmt:: Debug , V : fmt:: Debug > fmt:: Debug for Iter < ' a , K , V > {
1653
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1654
+ f. debug_list ( )
1655
+ . entries ( self . clone ( ) )
1656
+ . finish ( )
1657
+ }
1658
+ }
1659
+
1660
+ /// A mutable iterator over the entries of a `IndexMap`.
1661
+ ///
1662
+ /// This `struct` is created by the [`iter_mut`] method on [`IndexMap`]. See its
1663
+ /// documentation for more.
1664
+ ///
1665
+ /// [`iter_mut`]: struct.IndexMap.html#method.iter_mut
1666
+ /// [`IndexMap`]: struct.IndexMap.html
1546
1667
pub struct IterMut < ' a , K : ' a , V : ' a > {
1547
1668
iter : SliceIterMut < ' a , Bucket < K , V > > ,
1548
1669
}
@@ -1565,6 +1686,13 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
1565
1686
}
1566
1687
}
1567
1688
1689
+ /// An owning iterator over the entries of a `IndexMap`.
1690
+ ///
1691
+ /// This `struct` is created by the [`into_iter`] method on [`IndexMap`]
1692
+ /// (provided by the `IntoIterator` trait). See its documentation for more.
1693
+ ///
1694
+ /// [`into_iter`]: struct.IndexMap.html#method.into_iter
1695
+ /// [`IndexMap`]: struct.IndexMap.html
1568
1696
pub struct IntoIter < K , V > {
1569
1697
pub ( crate ) iter : VecIntoIter < Bucket < K , V > > ,
1570
1698
}
@@ -1587,6 +1715,20 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
1587
1715
}
1588
1716
}
1589
1717
1718
+ impl < K : fmt:: Debug , V : fmt:: Debug > fmt:: Debug for IntoIter < K , V > {
1719
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1720
+ let iter = self . iter . as_slice ( ) . iter ( ) . map ( Bucket :: refs) ;
1721
+ f. debug_list ( ) . entries ( iter) . finish ( )
1722
+ }
1723
+ }
1724
+
1725
+ /// A draining iterator over the entries of a `IndexMap`.
1726
+ ///
1727
+ /// This `struct` is created by the [`drain`] method on [`IndexMap`]. See its
1728
+ /// documentation for more.
1729
+ ///
1730
+ /// [`drain`]: struct.IndexMap.html#method.drain
1731
+ /// [`IndexMap`]: struct.IndexMap.html
1590
1732
pub struct Drain < ' a , K , V > where K : ' a , V : ' a {
1591
1733
pub ( crate ) iter : :: std:: vec:: Drain < ' a , Bucket < K , V > >
1592
1734
}
@@ -2047,4 +2189,40 @@ mod tests {
2047
2189
2048
2190
assert_eq ! ( & mut TestEnum :: DefaultValue , map. entry( 2 ) . or_default( ) ) ;
2049
2191
}
2192
+
2193
+ #[ test]
2194
+ fn keys ( ) {
2195
+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
2196
+ let map: IndexMap < _ , _ > = vec. into_iter ( ) . collect ( ) ;
2197
+ let keys: Vec < _ > = map. keys ( ) . cloned ( ) . collect ( ) ;
2198
+ assert_eq ! ( keys. len( ) , 3 ) ;
2199
+ assert ! ( keys. contains( & 1 ) ) ;
2200
+ assert ! ( keys. contains( & 2 ) ) ;
2201
+ assert ! ( keys. contains( & 3 ) ) ;
2202
+ }
2203
+
2204
+ #[ test]
2205
+ fn values ( ) {
2206
+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
2207
+ let map: IndexMap < _ , _ > = vec. into_iter ( ) . collect ( ) ;
2208
+ let values: Vec < _ > = map. values ( ) . cloned ( ) . collect ( ) ;
2209
+ assert_eq ! ( values. len( ) , 3 ) ;
2210
+ assert ! ( values. contains( & 'a' ) ) ;
2211
+ assert ! ( values. contains( & 'b' ) ) ;
2212
+ assert ! ( values. contains( & 'c' ) ) ;
2213
+ }
2214
+
2215
+ #[ test]
2216
+ fn values_mut ( ) {
2217
+ let vec = vec ! [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] ;
2218
+ let mut map: IndexMap < _ , _ > = vec. into_iter ( ) . collect ( ) ;
2219
+ for value in map. values_mut ( ) {
2220
+ * value = ( * value) * 2
2221
+ }
2222
+ let values: Vec < _ > = map. values ( ) . cloned ( ) . collect ( ) ;
2223
+ assert_eq ! ( values. len( ) , 3 ) ;
2224
+ assert ! ( values. contains( & 2 ) ) ;
2225
+ assert ! ( values. contains( & 4 ) ) ;
2226
+ assert ! ( values. contains( & 6 ) ) ;
2227
+ }
2050
2228
}
0 commit comments