Skip to content

Commit

Permalink
Auto merge of #99929 - the8472:default-iters, r=scottmcm
Browse files Browse the repository at this point in the history
Implement Default for some alloc/core iterators

Add `Default` impls to the following collection iterators:

* slice::{Iter, IterMut}
* binary_heap::IntoIter
* btree::map::{Iter, IterMut, Keys, Values, Range, IntoIter, IntoKeys, IntoValues}
* btree::set::{Iter, IntoIter, Range}
* linked_list::IntoIter
* vec::IntoIter

and these adapters:

* adapters::{Chain, Cloned, Copied, Rev, Enumerate, Flatten, Fuse, Rev}

For iterators which are generic over allocators it only implements it for the global allocator because we can't conjure an allocator from nothing or would have to turn the allocator field into an `Option` just for this change.

These changes will be insta-stable.

ACP: rust-lang/libs-team#77
  • Loading branch information
bors committed Mar 25, 2023
2 parents 6537178 + 3f56046 commit 90de60e
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
14 changes: 14 additions & 0 deletions collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,20 @@ impl<T> ExactSizeIterator for IntoIter<T> {
#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for IntoIter<T> {}

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

// In addition to the SAFETY invariants of the following three unsafe traits
// also refer to the vec::in_place_collect module documentation to get an overview
#[unstable(issue = "none", feature = "inplace_iteration")]
Expand Down
121 changes: 121 additions & 0 deletions collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,20 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
}
}

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

/// A mutable iterator over the entries of a `BTreeMap`.
///
/// This `struct` is created by the [`iter_mut`] method on [`BTreeMap`]. See its
Expand All @@ -386,6 +400,20 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
}
}

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

/// An owning iterator over the entries of a `BTreeMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`BTreeMap`]
Expand Down Expand Up @@ -421,6 +449,23 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for IntoIter<K, V, A> {
}
}

#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<K, V, A> Default for IntoIter<K, V, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_map::IntoIter`.
///
/// ```
/// # use std::collections::btree_map;
/// let iter: btree_map::IntoIter<u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoIter { range: Default::default(), length: 0, alloc: Default::default() }
}
}

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

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

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;
Expand Down Expand Up @@ -1809,6 +1868,20 @@ impl<K, V> Clone for Values<'_, K, V> {
}
}

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

/// An iterator produced by calling `drain_filter` on BTreeMap.
#[unstable(feature = "btree_drain_filter", issue = "70530")]
pub struct DrainFilter<
Expand Down Expand Up @@ -1945,6 +2018,20 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
}
}

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

#[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 @@ -2021,6 +2108,23 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoKeys<K, V, A> {
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
impl<K, V, A: Allocator + Clone> FusedIterator for IntoKeys<K, V, A> {}

#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<K, V, A> Default for IntoKeys<K, V, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_map::IntoKeys`.
///
/// ```
/// # use std::collections::btree_map;
/// let iter: btree_map::IntoKeys<u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoKeys { inner: Default::default() }
}
}

#[stable(feature = "map_into_keys_values", since = "1.54.0")]
impl<K, V, A: Allocator + Clone> Iterator for IntoValues<K, V, A> {
type Item = V;
Expand Down Expand Up @@ -2055,6 +2159,23 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoValues<K, V, A> {
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}

#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<K, V, A> Default for IntoValues<K, V, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_map::IntoValues`.
///
/// ```
/// # use std::collections::btree_map;
/// let iter: btree_map::IntoValues<u8, u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoValues { inner: Default::default() }
}
}

#[stable(feature = "btree_range", since = "1.17.0")]
impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
Expand Down
12 changes: 12 additions & 0 deletions collections/btree/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ impl<'a, K: 'a, V: 'a> Clone for LeafRange<marker::Immut<'a>, K, V> {
}
}

impl<B, K, V> Default for LeafRange<B, K, V> {
fn default() -> Self {
LeafRange { front: None, back: None }
}
}

impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
pub fn none() -> Self {
LeafRange { front: None, back: None }
Expand Down Expand Up @@ -124,6 +130,12 @@ pub struct LazyLeafRange<BorrowType, K, V> {
back: Option<LazyLeafHandle<BorrowType, K, V>>,
}

impl<B, K, V> Default for LazyLeafRange<B, K, V> {
fn default() -> Self {
LazyLeafRange { front: None, back: None }
}
}

impl<'a, K: 'a, V: 'a> Clone for LazyLeafRange<marker::Immut<'a>, K, V> {
fn clone(&self) -> Self {
LazyLeafRange { front: self.front.clone(), back: self.back.clone() }
Expand Down
46 changes: 46 additions & 0 deletions collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,21 @@ impl<T, A: Allocator + Clone> Iterator for IntoIter<T, A> {
self.iter.size_hint()
}
}

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

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator + Clone> DoubleEndedIterator for IntoIter<T, A> {
fn next_back(&mut self) -> Option<T> {
Expand All @@ -1555,6 +1570,23 @@ impl<T, A: Allocator + Clone> ExactSizeIterator for IntoIter<T, A> {
#[stable(feature = "fused", since = "1.26.0")]
impl<T, A: Allocator + Clone> FusedIterator for IntoIter<T, A> {}

#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<T, A> Default for IntoIter<T, A>
where
A: Allocator + Default + Clone,
{
/// Creates an empty `btree_set::IntoIter`.
///
/// ```
/// # use std::collections::btree_set;
/// let iter: btree_set::IntoIter<u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
IntoIter { iter: Default::default() }
}
}

#[stable(feature = "btree_range", since = "1.17.0")]
impl<T> Clone for Range<'_, T> {
fn clone(&self) -> Self {
Expand Down Expand Up @@ -1593,6 +1625,20 @@ impl<'a, T> DoubleEndedIterator for Range<'a, T> {
#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for Range<'_, T> {}

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

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator + Clone> Clone for Difference<'_, T, A> {
fn clone(&self) -> Self {
Expand Down

0 comments on commit 90de60e

Please sign in to comment.