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

Implement Default for some alloc/core iterators #99929

Merged
merged 4 commits into from
Mar 25, 2023
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 @@ -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 library/alloc/src/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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I would have guessed just

Suggested change
A: Allocator + Default + Clone,
A: Default,

but it's unstable and we can weaken it later, so meh.

{
/// 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 library/alloc/src/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 library/alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,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 @@ -1560,6 +1575,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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all these tests!

/// 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 @@ -1598,6 +1630,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
35 changes: 35 additions & 0 deletions library/alloc/src/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,20 @@ impl<T> ExactSizeIterator for Iter<'_, T> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for Iter<'_, T> {}

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

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
Expand Down Expand Up @@ -1129,6 +1143,13 @@ impl<T> ExactSizeIterator for IterMut<'_, T> {}
#[stable(feature = "fused", since = "1.26.0")]
impl<T> FusedIterator for IterMut<'_, T> {}

#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for IterMut<'_, T> {
fn default() -> Self {
IterMut { head: None, tail: None, len: 0, marker: Default::default() }
}
}

/// A cursor over a `LinkedList`.
///
/// A `Cursor` is like an iterator, except that it can freely seek back-and-forth.
Expand Down Expand Up @@ -1808,6 +1829,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 `linked_list::IntoIter`.
///
/// ```
/// # use std::collections::linked_list;
/// let iter: linked_list::IntoIter<u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// ```
fn default() -> Self {
LinkedList::new().into_iter()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> FromIterator<T> for LinkedList<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Expand Down
18 changes: 18 additions & 0 deletions library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,24 @@ impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}

#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<T, A> Default for IntoIter<T, A>
where
A: Allocator + Default,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not Clone for this one?

(But I guess again it's a fine "not this PR")

{
/// Creates an empty `vec::IntoIter`.
///
/// ```
/// # use std::vec;
/// let iter: vec::IntoIter<u8> = Default::default();
/// assert_eq!(iter.len(), 0);
/// assert_eq!(iter.as_slice(), &[]);
/// ```
fn default() -> Self {
super::Vec::new_in(Default::default()).into_iter()
}
}

#[doc(hidden)]
#[unstable(issue = "none", feature = "std_internals")]
#[rustc_unsafe_specialization_marker]
Expand Down
Loading