Skip to content

Commit d8e5225

Browse files
authored
Merge pull request #88 from Mingun/clone+debug
Implementation of Clone, Debug and documentation for structures
2 parents ee07695 + 33fd850 commit d8e5225

File tree

4 files changed

+317
-2
lines changed

4 files changed

+317
-2
lines changed

Cargo.toml

+2-2
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 <cuviper@gmail.com>"
@@ -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.7.0" # 0.8 not compiles on Rust 1.18
3535
rand = "0.4"
3636
quickcheck = { version = "0.6", default-features = false }
3737
fnv = "1.0"

README.rst

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ Ideas that we already did
108108
Recent Changes
109109
==============
110110

111+
- 1.1.0
112+
113+
- Implemented ``Clone`` for ``map::{Iter, Keys, Values}`` and
114+
``set::{Difference, Intersection, Iter, SymmetricDifference, Union}``
115+
116+
- Implemented ``Debug`` for ``map::{Entry, IntoIter, Iter, Keys, Values}`` and
117+
``set::{Difference, Intersection, IntoIter, Iter, SymmetricDifference, Union}``
118+
111119
- 1.0.2
112120

113121
- The new methods ``IndexMap::insert_full`` and ``IndexSet::insert_full`` are

src/map.rs

+178
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,20 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
15871715
}
15881716
}
15891717

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
15901732
pub struct Drain<'a, K, V> where K: 'a, V: 'a {
15911733
pub(crate) iter: ::std::vec::Drain<'a, Bucket<K, V>>
15921734
}
@@ -2047,4 +2189,40 @@ mod tests {
20472189

20482190
assert_eq!(&mut TestEnum::DefaultValue, map.entry(2).or_default());
20492191
}
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+
}
20502228
}

0 commit comments

Comments
 (0)