Skip to content

Commit 07df053

Browse files
committed
impl Default, Clone for Hash{Map,Set} iterators that don't already have it
1 parent f7eefec commit 07df053

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

library/std/src/collections/hash/map.rs

+67
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,14 @@ impl<K, V> Clone for Iter<'_, K, V> {
14041404
}
14051405
}
14061406

1407+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1408+
impl<K, V> Default for Iter<'_, K, V> {
1409+
#[inline]
1410+
fn default() -> Self {
1411+
Iter { base: Default::default() }
1412+
}
1413+
}
1414+
14071415
#[stable(feature = "std_debug", since = "1.16.0")]
14081416
impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
14091417
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -1441,6 +1449,14 @@ impl<'a, K, V> IterMut<'a, K, V> {
14411449
}
14421450
}
14431451

1452+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1453+
impl<K, V> Default for IterMut<'_, K, V> {
1454+
#[inline]
1455+
fn default() -> Self {
1456+
IterMut { base: Default::default() }
1457+
}
1458+
}
1459+
14441460
/// An owning iterator over the entries of a `HashMap`.
14451461
///
14461462
/// This `struct` is created by the [`into_iter`] method on [`HashMap`]
@@ -1458,6 +1474,7 @@ impl<'a, K, V> IterMut<'a, K, V> {
14581474
/// ]);
14591475
/// let iter = map.into_iter();
14601476
/// ```
1477+
#[derive(Clone)]
14611478
#[stable(feature = "rust1", since = "1.0.0")]
14621479
pub struct IntoIter<K, V> {
14631480
base: base::IntoIter<K, V>,
@@ -1471,6 +1488,14 @@ impl<K, V> IntoIter<K, V> {
14711488
}
14721489
}
14731490

1491+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1492+
impl<K, V> Default for IntoIter<K, V> {
1493+
#[inline]
1494+
fn default() -> Self {
1495+
IntoIter { base: Default::default() }
1496+
}
1497+
}
1498+
14741499
/// An iterator over the keys of a `HashMap`.
14751500
///
14761501
/// This `struct` is created by the [`keys`] method on [`HashMap`]. See its
@@ -1502,6 +1527,14 @@ impl<K, V> Clone for Keys<'_, K, V> {
15021527
}
15031528
}
15041529

1530+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1531+
impl<K, V> Default for Keys<'_, K, V> {
1532+
#[inline]
1533+
fn default() -> Self {
1534+
Keys { inner: Default::default() }
1535+
}
1536+
}
1537+
15051538
#[stable(feature = "std_debug", since = "1.16.0")]
15061539
impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
15071540
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -1540,6 +1573,14 @@ impl<K, V> Clone for Values<'_, K, V> {
15401573
}
15411574
}
15421575

1576+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1577+
impl<K, V> Default for Values<'_, K, V> {
1578+
#[inline]
1579+
fn default() -> Self {
1580+
Values { inner: Default::default() }
1581+
}
1582+
}
1583+
15431584
#[stable(feature = "std_debug", since = "1.16.0")]
15441585
impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
15451586
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -1626,6 +1667,14 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
16261667
inner: IterMut<'a, K, V>,
16271668
}
16281669

1670+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1671+
impl<K, V> Default for ValuesMut<'_, K, V> {
1672+
#[inline]
1673+
fn default() -> Self {
1674+
ValuesMut { inner: Default::default() }
1675+
}
1676+
}
1677+
16291678
/// An owning iterator over the keys of a `HashMap`.
16301679
///
16311680
/// This `struct` is created by the [`into_keys`] method on [`HashMap`].
@@ -1643,11 +1692,20 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
16431692
/// ]);
16441693
/// let iter_keys = map.into_keys();
16451694
/// ```
1695+
#[derive(Clone)]
16461696
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
16471697
pub struct IntoKeys<K, V> {
16481698
inner: IntoIter<K, V>,
16491699
}
16501700

1701+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1702+
impl<K, V> Default for IntoKeys<K, V> {
1703+
#[inline]
1704+
fn default() -> Self {
1705+
IntoKeys { inner: Default::default() }
1706+
}
1707+
}
1708+
16511709
/// An owning iterator over the values of a `HashMap`.
16521710
///
16531711
/// This `struct` is created by the [`into_values`] method on [`HashMap`].
@@ -1665,11 +1723,20 @@ pub struct IntoKeys<K, V> {
16651723
/// ]);
16661724
/// let iter_keys = map.into_values();
16671725
/// ```
1726+
#[derive(Clone)]
16681727
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
16691728
pub struct IntoValues<K, V> {
16701729
inner: IntoIter<K, V>,
16711730
}
16721731

1732+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1733+
impl<K, V> Default for IntoValues<K, V> {
1734+
#[inline]
1735+
fn default() -> Self {
1736+
IntoValues { inner: Default::default() }
1737+
}
1738+
}
1739+
16731740
/// A builder for computing where in a HashMap a key-value pair would be stored.
16741741
///
16751742
/// See the [`HashMap::raw_entry_mut`] docs for usage examples.

library/std/src/collections/hash/set.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,14 @@ pub struct Iter<'a, K: 'a> {
12741274
base: base::Iter<'a, K>,
12751275
}
12761276

1277+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1278+
impl<K> Default for Iter<'_, K> {
1279+
#[inline]
1280+
fn default() -> Self {
1281+
Iter { base: Default::default() }
1282+
}
1283+
}
1284+
12771285
/// An owning iterator over the items of a `HashSet`.
12781286
///
12791287
/// This `struct` is created by the [`into_iter`] method on [`HashSet`]
@@ -1295,6 +1303,14 @@ pub struct IntoIter<K> {
12951303
base: base::IntoIter<K>,
12961304
}
12971305

1306+
#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1307+
impl<K> Default for IntoIter<K> {
1308+
#[inline]
1309+
fn default() -> Self {
1310+
IntoIter { base: Default::default() }
1311+
}
1312+
}
1313+
12981314
/// A draining iterator over the items of a `HashSet`.
12991315
///
13001316
/// This `struct` is created by the [`drain`] method on [`HashSet`].
@@ -1536,6 +1552,13 @@ impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> {
15361552
}
15371553
}
15381554

1555+
#[stable(feature = "clone_iters_hash", since = "CURRENT_RUSTC_VERSION")]
1556+
impl<K> Clone for IntoIter<K> {
1557+
#[inline]
1558+
fn clone(&self) -> Self {
1559+
IntoIter { base: self.base.clone() }
1560+
}
1561+
}
15391562
#[stable(feature = "rust1", since = "1.0.0")]
15401563
impl<K> Iterator for IntoIter<K> {
15411564
type Item = K;

0 commit comments

Comments
 (0)