Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Default for collection iterators that don't already have it #128261

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading