Skip to content

Commit

Permalink
Rollup merge of #128261 - clarfonthey:iter-default, r=dtolnay
Browse files Browse the repository at this point in the history
impl `Default` for collection iterators that don't already have it

There is a pretty strong precedent for implementing `Default` for collection iterators, and this does so for some where this implementation was missed.

I don't think this needs a separate ACP (since this precedent already exists, and these feel like they were just missed), however, it *will* need an FCP since these implementations are instantly stable.
  • Loading branch information
matthiaskrgr authored Aug 7, 2024
2 parents 904f579 + 0b99720 commit 2ee9678
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions library/alloc/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,20 @@ pub struct Iter<'a, T: 'a> {
iter: slice::Iter<'a, T>,
}

#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for Iter<'_, T> {
/// Creates an empty `binary_heap::Iter`.
///
/// ```
/// # use std::collections::binary_heap;
/// let iter: binary_heap::Iter<'_, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
Iter { iter: Default::default() }
}
}

#[stable(feature = "collection_debug", since = "1.17.0")]
impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
28 changes: 28 additions & 0 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,20 @@ impl<K, V> Default for Range<'_, K, V> {
}
}

#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for RangeMut<'_, K, V> {
/// Creates an empty `btree_map::RangeMut`.
///
/// ```
/// # use std::collections::btree_map;
/// let iter: btree_map::RangeMut<'_, u8, u8> = Default::default();
/// assert_eq!(iter.count(), 0);
/// ```
fn default() -> Self {
RangeMut { inner: Default::default(), _marker: PhantomData }
}
}

#[stable(feature = "map_values_mut", since = "1.10.0")]
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;
Expand Down Expand Up @@ -2050,6 +2064,20 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
#[stable(feature = "fused", since = "1.26.0")]
impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}

#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for ValuesMut<'_, K, V> {
/// Creates an empty `btree_map::ValuesMut`.
///
/// ```
/// # use std::collections::btree_map;
/// let iter: btree_map::ValuesMut<'_, u8, u8> = Default::default();
/// assert_eq!(iter.count(), 0);
/// ```
fn default() -> Self {
ValuesMut { inner: Default::default() }
}
}

#[stable(feature = "map_into_keys_values", since = "1.54.0")]
impl<K, V, A: Allocator + Clone> Iterator for IntoKeys<K, V, A> {
type Item = K;
Expand Down
14 changes: 14 additions & 0 deletions library/alloc/src/collections/vec_deque/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
}
}

#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for Iter<'_, T> {
/// Creates an empty `vec_deque::Iter`.
///
/// ```
/// # use std::collections::vec_deque;
/// let iter: vec_deque::Iter<'_, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
Iter { i1: Default::default(), i2: Default::default() }
}
}

// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Clone for Iter<'_, T> {
Expand Down
14 changes: 14 additions & 0 deletions library/alloc/src/collections/vec_deque/iter_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
}
}

#[stable(feature = "default_iters_sequel", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for IterMut<'_, T> {
/// Creates an empty `vec_deque::IterMut`.
///
/// ```
/// # use std::collections::vec_deque;
/// let iter: vec_deque::IterMut<'_, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IterMut { i1: Default::default(), i2: Default::default() }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
Expand Down

0 comments on commit 2ee9678

Please sign in to comment.