Skip to content

Commit ba1e2c9

Browse files
committed
IndexMap: Implement Clone, Debug and add documentation for structures
Clone for: Keys, Values, Iter Debug for: Keys, Values, Iter, Entry Docs for: Keys, Values, Iter, Entry, ValuesMut, IterMut, IntoIter, Drain All copied from HashMap
1 parent ee07695 commit ba1e2c9

File tree

2 files changed

+173
-2
lines changed

2 files changed

+173
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "indexmap"
3-
version = "1.0.2"
3+
version = "1.1.0"
44
authors = [
55
"bluss",
66
"Josh Stone <[email protected]>"
@@ -31,7 +31,7 @@ bench = false
3131
serde = { version = "1.0", optional = true }
3232

3333
[dev-dependencies]
34-
itertools = "0.7.0"
34+
itertools = "0.8.0"
3535
rand = "0.4"
3636
quickcheck = { version = "0.6", default-features = false }
3737
fnv = "1.0"

src/map.rs

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,27 @@ impl<'a, K, V> Entry<'a, K, V> {
580580
}
581581
}
582582

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
583604
pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
584605
map: &'a mut OrderMapCore<K, V>,
585606
key: K,
@@ -625,7 +646,19 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
625646
}
626647
}
627648

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+
}
628657

658+
/// A view into a vacant entry in a `IndexMap`.
659+
/// It is part of the [`Entry`] enum.
660+
///
661+
/// [`Entry`]: enum.Entry.html
629662
pub struct VacantEntry<'a, K: 'a, V: 'a> {
630663
map: &'a mut OrderMapCore<K, V>,
631664
key: K,
@@ -657,6 +690,14 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
657690
}
658691
}
659692

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+
660701
impl<K, V, S> IndexMap<K, V, S>
661702
where K: Hash + Eq,
662703
S: BuildHasher,
@@ -1455,6 +1496,13 @@ use std::slice::Iter as SliceIter;
14551496
use std::slice::IterMut as SliceIterMut;
14561497
use std::vec::IntoIter as VecIntoIter;
14571498

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
14581506
pub struct Keys<'a, K: 'a, V: 'a> {
14591507
pub(crate) iter: SliceIter<'a, Bucket<K, V>>,
14601508
}
@@ -1477,6 +1525,28 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
14771525
}
14781526
}
14791527

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
14801550
pub struct Values<'a, K: 'a, V: 'a> {
14811551
iter: SliceIter<'a, Bucket<K, V>>,
14821552
}
@@ -1499,6 +1569,28 @@ impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
14991569
}
15001570
}
15011571

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
15021594
pub struct ValuesMut<'a, K: 'a, V: 'a> {
15031595
iter: SliceIterMut<'a, Bucket<K, V>>,
15041596
}
@@ -1521,6 +1613,13 @@ impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> {
15211613
}
15221614
}
15231615

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
15241623
pub struct Iter<'a, K: 'a, V: 'a> {
15251624
iter: SliceIter<'a, Bucket<K, V>>,
15261625
}
@@ -1543,6 +1642,28 @@ impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
15431642
}
15441643
}
15451644

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
15461667
pub struct IterMut<'a, K: 'a, V: 'a> {
15471668
iter: SliceIterMut<'a, Bucket<K, V>>,
15481669
}
@@ -1565,6 +1686,13 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
15651686
}
15661687
}
15671688

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
15681696
pub struct IntoIter<K, V> {
15691697
pub(crate) iter: VecIntoIter<Bucket<K, V>>,
15701698
}
@@ -1587,6 +1715,13 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
15871715
}
15881716
}
15891717

1718+
/// A draining iterator over the entries of a `IndexMap`.
1719+
///
1720+
/// This `struct` is created by the [`drain`] method on [`IndexMap`]. See its
1721+
/// documentation for more.
1722+
///
1723+
/// [`drain`]: struct.IndexMap.html#method.drain
1724+
/// [`IndexMap`]: struct.IndexMap.html
15901725
pub struct Drain<'a, K, V> where K: 'a, V: 'a {
15911726
pub(crate) iter: ::std::vec::Drain<'a, Bucket<K, V>>
15921727
}
@@ -2047,4 +2182,40 @@ mod tests {
20472182

20482183
assert_eq!(&mut TestEnum::DefaultValue, map.entry(2).or_default());
20492184
}
2185+
2186+
#[test]
2187+
fn keys() {
2188+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
2189+
let map: IndexMap<_, _> = vec.into_iter().collect();
2190+
let keys: Vec<_> = map.keys().cloned().collect();
2191+
assert_eq!(keys.len(), 3);
2192+
assert!(keys.contains(&1));
2193+
assert!(keys.contains(&2));
2194+
assert!(keys.contains(&3));
2195+
}
2196+
2197+
#[test]
2198+
fn values() {
2199+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
2200+
let map: IndexMap<_, _> = vec.into_iter().collect();
2201+
let values: Vec<_> = map.values().cloned().collect();
2202+
assert_eq!(values.len(), 3);
2203+
assert!(values.contains(&'a'));
2204+
assert!(values.contains(&'b'));
2205+
assert!(values.contains(&'c'));
2206+
}
2207+
2208+
#[test]
2209+
fn values_mut() {
2210+
let vec = vec![(1, 1), (2, 2), (3, 3)];
2211+
let mut map: IndexMap<_, _> = vec.into_iter().collect();
2212+
for value in map.values_mut() {
2213+
*value = (*value) * 2
2214+
}
2215+
let values: Vec<_> = map.values().cloned().collect();
2216+
assert_eq!(values.len(), 3);
2217+
assert!(values.contains(&2));
2218+
assert!(values.contains(&4));
2219+
assert!(values.contains(&6));
2220+
}
20502221
}

0 commit comments

Comments
 (0)