Implement Default for some alloc/core iterators

This way one can `mem::take()` them out of structs or #[derive(Default)] on structs containing them.

These changes will be insta-stable.
This commit is contained in:
The 8472
2022-07-30 01:48:16 +02:00
parent 31f858d9a5
commit 05c7330ca0
18 changed files with 243 additions and 2 deletions
@@ -1468,6 +1468,13 @@ fn is_empty(&self) -> bool {
#[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> {
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")]
@@ -362,6 +362,13 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<'a, K: 'a, V: 'a> Default for Iter<'a, K, V> {
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
@@ -386,6 +393,13 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<'a, K: 'a, V: 'a> Default for IterMut<'a, K, V> {
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`]
@@ -421,6 +435,13 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for IntoIter<K, V> {
fn default() -> Self {
IntoIter { range: Default::default(), length: 0, alloc: Global }
}
}
/// An iterator over the keys of a `BTreeMap`.
///
/// This `struct` is created by the [`keys`] method on [`BTreeMap`]. See its
@@ -1768,6 +1789,13 @@ fn clone(&self) -> Self {
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for Keys<'_, K, V> {
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;
@@ -1809,6 +1837,13 @@ fn clone(&self) -> Self {
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for Values<'_, K, V> {
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<
@@ -1945,6 +1980,13 @@ fn max(mut self) -> Option<(&'a K, &'a V)> {
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<K, V> Default for Range<'_, K, V> {
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;
@@ -2021,6 +2063,13 @@ fn len(&self) -> usize {
#[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> Default for IntoKeys<K, V> {
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;
@@ -2055,6 +2104,13 @@ fn len(&self) -> usize {
#[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> Default for IntoValues<K, V> {
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)> {
@@ -563,6 +563,22 @@ fn test_iter_min_max() {
a.check();
}
#[test]
fn test_iters_default() {
let iter: Keys<'_, u8, u8> = Keys::default();
assert_eq!(iter.len(), 0);
let iter: Values<'_, u8, u8> = Values::default();
assert_eq!(iter.len(), 0);
let iter: Range<'_, u8, u8> = Range::default();
assert_eq!(iter.count(), 0);
let iter: IntoIter<u8, u8> = IntoIter::default();
assert_eq!(iter.len(), 0);
let iter: IntoKeys<u8, u8> = IntoKeys::default();
assert_eq!(iter.len(), 0);
let iter: IntoValues<u8, u8> = IntoValues::default();
assert_eq!(iter.len(), 0);
}
fn range_keys(map: &BTreeMap<i32, i32>, range: impl RangeBounds<i32>) -> Vec<i32> {
Vec::from_iter(map.range(range).map(|(&k, &v)| {
assert_eq!(k, v);
@@ -19,6 +19,12 @@ fn clone(&self) -> Self {
}
}
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 }
@@ -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() }
@@ -1544,6 +1544,14 @@ fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for Iter<'_, T> {
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> {
@@ -1560,6 +1568,13 @@ fn len(&self) -> usize {
#[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> Default for IntoIter<T> {
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 {
@@ -1598,6 +1613,13 @@ fn next_back(&mut self) -> Option<&'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> {
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 {
@@ -1075,6 +1075,13 @@ 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> {
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;
@@ -1129,6 +1136,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.
@@ -1808,6 +1822,13 @@ 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> {
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 {
@@ -172,6 +172,12 @@ fn test_iterator() {
assert_eq!(it.next(), None);
}
#[test]
fn test_default() {
let iter: IntoIter<u8> = Default::default();
assert_eq!(iter.len(), 0);
}
#[test]
fn test_iterator_clone() {
let mut n = LinkedList::new();
+7
View File
@@ -347,6 +347,13 @@ 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> Default for IntoIter<T> {
fn default() -> Self {
super::Vec::new().into_iter()
}
}
#[doc(hidden)]
#[unstable(issue = "none", feature = "std_internals")]
#[rustc_unsafe_specialization_marker]
+9 -1
View File
@@ -1,5 +1,6 @@
use core::alloc::{Allocator, Layout};
use core::iter::IntoIterator;
use core::assert_eq;
use core::iter::{ExactSizeIterator, IntoIterator};
use core::ptr::NonNull;
use std::alloc::System;
use std::assert_matches::assert_matches;
@@ -1035,6 +1036,13 @@ fn iter_equal<I: Iterator<Item = i32>>(it: I, slice: &[i32]) {
assert_eq!(it.next(), None);
}
#[test]
fn test_into_iter_default() {
let iter: IntoIter<u8> = Default::default();
assert_eq!(iter.len(), 0);
assert_eq!(iter.as_slice(), &[]);
}
#[test]
fn test_into_iter_leak() {
static mut DROPS: i32 = 0;
+11
View File
@@ -282,6 +282,17 @@ unsafe impl<A, B> TrustedLen for Chain<A, B>
{
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<A, B> Default for Chain<A, B>
where
A: Iterator + Default,
B: Iterator + Default,
{
fn default() -> Self {
Chain::new(Default::default(), Default::default())
}
}
#[inline]
fn and_then_or_clear<T, U>(opt: &mut Option<T>, f: impl FnOnce(&mut T) -> Option<U>) -> Option<U> {
let x = f(opt.as_mut()?);
+11
View File
@@ -153,3 +153,14 @@ unsafe fn next_unchecked(&mut self) -> T {
item.clone()
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<'a, I, T: 'a> Default for Cloned<I>
where
I: Default + Iterator<Item = &'a T>,
T: Clone,
{
fn default() -> Self {
Self::new(Default::default())
}
}
+11
View File
@@ -240,3 +240,14 @@ impl<'a, const N: usize, T: 'a> SpecNextChunk<'a, N, T> for crate::slice::Iter<'
}
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<'a, I, T: 'a> Default for Copied<I>
where
I: Default + Iterator<Item = &'a T>,
T: Copy,
{
fn default() -> Self {
Self::new(Default::default())
}
}
@@ -264,3 +264,13 @@ unsafe fn as_inner(&mut self) -> &mut I::Source {
#[unstable(issue = "none", feature = "inplace_iteration")]
unsafe impl<I: InPlaceIterable> InPlaceIterable for Enumerate<I> {}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<I> Default for Enumerate<I>
where
I: Iterator + Default,
{
fn default() -> Self {
Enumerate::new(Default::default())
}
}
+11
View File
@@ -302,6 +302,17 @@ unsafe impl<I> TrustedLen for Flatten<I>
{
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<I> Default for Flatten<I>
where
I: Iterator + Default,
<I as Iterator>::Item: IntoIterator,
{
fn default() -> Self {
Flatten::new(Default::default())
}
}
/// Real logic of both `Flatten` and `FlatMap` which simply delegate to
/// this type.
#[derive(Clone, Debug)]
+7
View File
@@ -181,6 +181,13 @@ fn is_empty(&self) -> bool {
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<I: Default> Default for Fuse<I> {
fn default() -> Self {
Fuse { iter: Default::default() }
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
// SAFETY: `TrustedLen` requires that an accurate length is reported via `size_hint()`. As `Fuse`
// is just forwarding this to the wrapped iterator `I` this property is preserved and it is safe to
+10
View File
@@ -135,3 +135,13 @@ impl<I> FusedIterator for Rev<I> where I: FusedIterator + DoubleEndedIterator {}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<I> TrustedLen for Rev<I> where I: TrustedLen + DoubleEndedIterator {}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<I> Default for Rev<I>
where
I: Default + Iterator,
{
fn default() -> Self {
Rev::new(Default::default())
}
}
+7
View File
@@ -393,6 +393,13 @@ unsafe fn next_unchecked(&mut self) -> $elem {
}
}
}
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for $name<'_, T> {
fn default() -> Self {
(& $( $mut_ )? []).into_iter()
}
}
}
}
+9 -1
View File
@@ -1,8 +1,10 @@
use core::cell::Cell;
use core::cmp::Ordering;
use core::iter::ExactSizeIterator;
use core::mem::MaybeUninit;
use core::result::Result::{Err, Ok};
use core::slice;
use core::slice::Iter;
use core::{assert_eq, slice};
#[test]
fn test_position() {
@@ -224,6 +226,12 @@ fn test_iterator_count() {
assert_eq!(iter2.count(), 3);
}
#[test]
fn test_iterator_default() {
let iter: Iter<'_, u8> = Iter::default();
assert_eq!(iter.len(), 0);
}
#[test]
fn test_chunks_count() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];