Skip to content

Commit

Permalink
impl Default, Clone for Hash{Map,Set} iterators that don't already ha…
Browse files Browse the repository at this point in the history
…ve it
  • Loading branch information
clarfonthey committed Aug 5, 2024
1 parent f7eefec commit 07df053
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
67 changes: 67 additions & 0 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,14 @@ impl<K, V> Clone for Iter<'_, K, V> {
}
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for Iter<'_, K, V> {
#[inline]
fn default() -> Self {
Iter { base: Default::default() }
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -1441,6 +1449,14 @@ impl<'a, K, V> IterMut<'a, K, V> {
}
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for IterMut<'_, K, V> {
#[inline]
fn default() -> Self {
IterMut { base: Default::default() }
}
}

/// An owning iterator over the entries of a `HashMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`HashMap`]
Expand All @@ -1458,6 +1474,7 @@ impl<'a, K, V> IterMut<'a, K, V> {
/// ]);
/// let iter = map.into_iter();
/// ```
#[derive(Clone)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<K, V> {
base: base::IntoIter<K, V>,
Expand All @@ -1471,6 +1488,14 @@ impl<K, V> IntoIter<K, V> {
}
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for IntoIter<K, V> {
#[inline]
fn default() -> Self {
IntoIter { base: Default::default() }
}
}

/// An iterator over the keys of a `HashMap`.
///
/// This `struct` is created by the [`keys`] method on [`HashMap`]. See its
Expand Down Expand Up @@ -1502,6 +1527,14 @@ impl<K, V> Clone for Keys<'_, K, V> {
}
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for Keys<'_, K, V> {
#[inline]
fn default() -> Self {
Keys { inner: Default::default() }
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -1540,6 +1573,14 @@ impl<K, V> Clone for Values<'_, K, V> {
}
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for Values<'_, K, V> {
#[inline]
fn default() -> Self {
Values { inner: Default::default() }
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -1626,6 +1667,14 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V>,
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for ValuesMut<'_, K, V> {
#[inline]
fn default() -> Self {
ValuesMut { inner: Default::default() }
}
}

/// An owning iterator over the keys of a `HashMap`.
///
/// This `struct` is created by the [`into_keys`] method on [`HashMap`].
Expand All @@ -1643,11 +1692,20 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
/// ]);
/// let iter_keys = map.into_keys();
/// ```
#[derive(Clone)]
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub struct IntoKeys<K, V> {
inner: IntoIter<K, V>,
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for IntoKeys<K, V> {
#[inline]
fn default() -> Self {
IntoKeys { inner: Default::default() }
}
}

/// An owning iterator over the values of a `HashMap`.
///
/// This `struct` is created by the [`into_values`] method on [`HashMap`].
Expand All @@ -1665,11 +1723,20 @@ pub struct IntoKeys<K, V> {
/// ]);
/// let iter_keys = map.into_values();
/// ```
#[derive(Clone)]
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
pub struct IntoValues<K, V> {
inner: IntoIter<K, V>,
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for IntoValues<K, V> {
#[inline]
fn default() -> Self {
IntoValues { inner: Default::default() }
}
}

/// A builder for computing where in a HashMap a key-value pair would be stored.
///
/// See the [`HashMap::raw_entry_mut`] docs for usage examples.
Expand Down
23 changes: 23 additions & 0 deletions library/std/src/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,14 @@ pub struct Iter<'a, K: 'a> {
base: base::Iter<'a, K>,
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K> Default for Iter<'_, K> {
#[inline]
fn default() -> Self {
Iter { base: Default::default() }
}
}

/// An owning iterator over the items of a `HashSet`.
///
/// This `struct` is created by the [`into_iter`] method on [`HashSet`]
Expand All @@ -1295,6 +1303,14 @@ pub struct IntoIter<K> {
base: base::IntoIter<K>,
}

#[stable(feature = "default_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K> Default for IntoIter<K> {
#[inline]
fn default() -> Self {
IntoIter { base: Default::default() }
}
}

/// A draining iterator over the items of a `HashSet`.
///
/// This `struct` is created by the [`drain`] method on [`HashSet`].
Expand Down Expand Up @@ -1536,6 +1552,13 @@ impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> {
}
}

#[stable(feature = "clone_iters_hash", since = "CURRENT_RUSTC_VERSION")]
impl<K> Clone for IntoIter<K> {
#[inline]
fn clone(&self) -> Self {
IntoIter { base: self.base.clone() }
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<K> Iterator for IntoIter<K> {
type Item = K;
Expand Down

0 comments on commit 07df053

Please sign in to comment.