Skip to content

Commit 05c7330

Browse files
committed
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.
1 parent 31f858d commit 05c7330

File tree

18 files changed

+243
-2
lines changed

18 files changed

+243
-2
lines changed

library/alloc/src/collections/binary_heap/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,13 @@ impl<T> ExactSizeIterator for IntoIter<T> {
14681468
#[stable(feature = "fused", since = "1.26.0")]
14691469
impl<T> FusedIterator for IntoIter<T> {}
14701470

1471+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1472+
impl<T> Default for IntoIter<T> {
1473+
fn default() -> Self {
1474+
IntoIter { iter: Default::default() }
1475+
}
1476+
}
1477+
14711478
// In addition to the SAFETY invariants of the following three unsafe traits
14721479
// also refer to the vec::in_place_collect module documentation to get an overview
14731480
#[unstable(issue = "none", feature = "inplace_iteration")]

library/alloc/src/collections/btree/map.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,13 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
362362
}
363363
}
364364

365+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
366+
impl<'a, K: 'a, V: 'a> Default for Iter<'a, K, V> {
367+
fn default() -> Self {
368+
Iter { range: Default::default(), length: 0 }
369+
}
370+
}
371+
365372
/// A mutable iterator over the entries of a `BTreeMap`.
366373
///
367374
/// This `struct` is created by the [`iter_mut`] method on [`BTreeMap`]. See its
@@ -386,6 +393,13 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
386393
}
387394
}
388395

396+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
397+
impl<'a, K: 'a, V: 'a> Default for IterMut<'a, K, V> {
398+
fn default() -> Self {
399+
IterMut { range: Default::default(), length: 0, _marker: PhantomData {} }
400+
}
401+
}
402+
389403
/// An owning iterator over the entries of a `BTreeMap`.
390404
///
391405
/// This `struct` is created by the [`into_iter`] method on [`BTreeMap`]
@@ -421,6 +435,13 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for IntoIter<K, V, A> {
421435
}
422436
}
423437

438+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
439+
impl<K, V> Default for IntoIter<K, V> {
440+
fn default() -> Self {
441+
IntoIter { range: Default::default(), length: 0, alloc: Global }
442+
}
443+
}
444+
424445
/// An iterator over the keys of a `BTreeMap`.
425446
///
426447
/// This `struct` is created by the [`keys`] method on [`BTreeMap`]. See its
@@ -1768,6 +1789,13 @@ impl<K, V> Clone for Keys<'_, K, V> {
17681789
}
17691790
}
17701791

1792+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1793+
impl<K, V> Default for Keys<'_, K, V> {
1794+
fn default() -> Self {
1795+
Keys { inner: Default::default() }
1796+
}
1797+
}
1798+
17711799
#[stable(feature = "rust1", since = "1.0.0")]
17721800
impl<'a, K, V> Iterator for Values<'a, K, V> {
17731801
type Item = &'a V;
@@ -1809,6 +1837,13 @@ impl<K, V> Clone for Values<'_, K, V> {
18091837
}
18101838
}
18111839

1840+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1841+
impl<K, V> Default for Values<'_, K, V> {
1842+
fn default() -> Self {
1843+
Values { inner: Default::default() }
1844+
}
1845+
}
1846+
18121847
/// An iterator produced by calling `drain_filter` on BTreeMap.
18131848
#[unstable(feature = "btree_drain_filter", issue = "70530")]
18141849
pub struct DrainFilter<
@@ -1945,6 +1980,13 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
19451980
}
19461981
}
19471982

1983+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1984+
impl<K, V> Default for Range<'_, K, V> {
1985+
fn default() -> Self {
1986+
Range { inner: Default::default() }
1987+
}
1988+
}
1989+
19481990
#[stable(feature = "map_values_mut", since = "1.10.0")]
19491991
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
19501992
type Item = &'a mut V;
@@ -2021,6 +2063,13 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoKeys<K, V, A> {
20212063
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
20222064
impl<K, V, A: Allocator + Clone> FusedIterator for IntoKeys<K, V, A> {}
20232065

2066+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
2067+
impl<K, V> Default for IntoKeys<K, V> {
2068+
fn default() -> Self {
2069+
IntoKeys { inner: Default::default() }
2070+
}
2071+
}
2072+
20242073
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
20252074
impl<K, V, A: Allocator + Clone> Iterator for IntoValues<K, V, A> {
20262075
type Item = V;
@@ -2055,6 +2104,13 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoValues<K, V, A> {
20552104
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
20562105
impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}
20572106

2107+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
2108+
impl<K, V> Default for IntoValues<K, V> {
2109+
fn default() -> Self {
2110+
IntoValues { inner: Default::default() }
2111+
}
2112+
}
2113+
20582114
#[stable(feature = "btree_range", since = "1.17.0")]
20592115
impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
20602116
fn next_back(&mut self) -> Option<(&'a K, &'a V)> {

library/alloc/src/collections/btree/map/tests.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,22 @@ fn test_iter_min_max() {
563563
a.check();
564564
}
565565

566+
#[test]
567+
fn test_iters_default() {
568+
let iter: Keys<'_, u8, u8> = Keys::default();
569+
assert_eq!(iter.len(), 0);
570+
let iter: Values<'_, u8, u8> = Values::default();
571+
assert_eq!(iter.len(), 0);
572+
let iter: Range<'_, u8, u8> = Range::default();
573+
assert_eq!(iter.count(), 0);
574+
let iter: IntoIter<u8, u8> = IntoIter::default();
575+
assert_eq!(iter.len(), 0);
576+
let iter: IntoKeys<u8, u8> = IntoKeys::default();
577+
assert_eq!(iter.len(), 0);
578+
let iter: IntoValues<u8, u8> = IntoValues::default();
579+
assert_eq!(iter.len(), 0);
580+
}
581+
566582
fn range_keys(map: &BTreeMap<i32, i32>, range: impl RangeBounds<i32>) -> Vec<i32> {
567583
Vec::from_iter(map.range(range).map(|(&k, &v)| {
568584
assert_eq!(k, v);

library/alloc/src/collections/btree/navigate.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ impl<'a, K: 'a, V: 'a> Clone for LeafRange<marker::Immut<'a>, K, V> {
1919
}
2020
}
2121

22+
impl<B, K, V> Default for LeafRange<B, K, V> {
23+
fn default() -> Self {
24+
LeafRange { front: None, back: None }
25+
}
26+
}
27+
2228
impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
2329
pub fn none() -> Self {
2430
LeafRange { front: None, back: None }
@@ -124,6 +130,12 @@ pub struct LazyLeafRange<BorrowType, K, V> {
124130
back: Option<LazyLeafHandle<BorrowType, K, V>>,
125131
}
126132

133+
impl<B, K, V> Default for LazyLeafRange<B, K, V> {
134+
fn default() -> Self {
135+
LazyLeafRange { front: None, back: None }
136+
}
137+
}
138+
127139
impl<'a, K: 'a, V: 'a> Clone for LazyLeafRange<marker::Immut<'a>, K, V> {
128140
fn clone(&self) -> Self {
129141
LazyLeafRange { front: self.front.clone(), back: self.back.clone() }

library/alloc/src/collections/btree/set.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,14 @@ impl<T, A: Allocator + Clone> Iterator for IntoIter<T, A> {
15441544
self.iter.size_hint()
15451545
}
15461546
}
1547+
1548+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1549+
impl<T> Default for Iter<'_, T> {
1550+
fn default() -> Self {
1551+
Iter { iter: Default::default() }
1552+
}
1553+
}
1554+
15471555
#[stable(feature = "rust1", since = "1.0.0")]
15481556
impl<T, A: Allocator + Clone> DoubleEndedIterator for IntoIter<T, A> {
15491557
fn next_back(&mut self) -> Option<T> {
@@ -1560,6 +1568,13 @@ impl<T, A: Allocator + Clone> ExactSizeIterator for IntoIter<T, A> {
15601568
#[stable(feature = "fused", since = "1.26.0")]
15611569
impl<T, A: Allocator + Clone> FusedIterator for IntoIter<T, A> {}
15621570

1571+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1572+
impl<T> Default for IntoIter<T> {
1573+
fn default() -> Self {
1574+
IntoIter { iter: Default::default() }
1575+
}
1576+
}
1577+
15631578
#[stable(feature = "btree_range", since = "1.17.0")]
15641579
impl<T> Clone for Range<'_, T> {
15651580
fn clone(&self) -> Self {
@@ -1598,6 +1613,13 @@ impl<'a, T> DoubleEndedIterator for Range<'a, T> {
15981613
#[stable(feature = "fused", since = "1.26.0")]
15991614
impl<T> FusedIterator for Range<'_, T> {}
16001615

1616+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1617+
impl<T> Default for Range<'_, T> {
1618+
fn default() -> Self {
1619+
Range { iter: Default::default() }
1620+
}
1621+
}
1622+
16011623
#[stable(feature = "rust1", since = "1.0.0")]
16021624
impl<T, A: Allocator + Clone> Clone for Difference<'_, T, A> {
16031625
fn clone(&self) -> Self {

library/alloc/src/collections/linked_list.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,13 @@ impl<T> ExactSizeIterator for Iter<'_, T> {}
10751075
#[stable(feature = "fused", since = "1.26.0")]
10761076
impl<T> FusedIterator for Iter<'_, T> {}
10771077

1078+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1079+
impl<T> Default for Iter<'_, T> {
1080+
fn default() -> Self {
1081+
Iter { head: None, tail: None, len: 0, marker: Default::default() }
1082+
}
1083+
}
1084+
10781085
#[stable(feature = "rust1", since = "1.0.0")]
10791086
impl<'a, T> Iterator for IterMut<'a, T> {
10801087
type Item = &'a mut T;
@@ -1129,6 +1136,13 @@ impl<T> ExactSizeIterator for IterMut<'_, T> {}
11291136
#[stable(feature = "fused", since = "1.26.0")]
11301137
impl<T> FusedIterator for IterMut<'_, T> {}
11311138

1139+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1140+
impl<T> Default for IterMut<'_, T> {
1141+
fn default() -> Self {
1142+
IterMut { head: None, tail: None, len: 0, marker: Default::default() }
1143+
}
1144+
}
1145+
11321146
/// A cursor over a `LinkedList`.
11331147
///
11341148
/// 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> {}
18081822
#[stable(feature = "fused", since = "1.26.0")]
18091823
impl<T> FusedIterator for IntoIter<T> {}
18101824

1825+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1826+
impl<T> Default for IntoIter<T> {
1827+
fn default() -> Self {
1828+
LinkedList::new().into_iter()
1829+
}
1830+
}
1831+
18111832
#[stable(feature = "rust1", since = "1.0.0")]
18121833
impl<T> FromIterator<T> for LinkedList<T> {
18131834
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {

library/alloc/src/collections/linked_list/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ fn test_iterator() {
172172
assert_eq!(it.next(), None);
173173
}
174174

175+
#[test]
176+
fn test_default() {
177+
let iter: IntoIter<u8> = Default::default();
178+
assert_eq!(iter.len(), 0);
179+
}
180+
175181
#[test]
176182
fn test_iterator_clone() {
177183
let mut n = LinkedList::new();

library/alloc/src/vec/into_iter.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}
347347
#[unstable(feature = "trusted_len", issue = "37572")]
348348
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}
349349

350+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
351+
impl<T> Default for IntoIter<T> {
352+
fn default() -> Self {
353+
super::Vec::new().into_iter()
354+
}
355+
}
356+
350357
#[doc(hidden)]
351358
#[unstable(issue = "none", feature = "std_internals")]
352359
#[rustc_unsafe_specialization_marker]

library/alloc/tests/vec.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::alloc::{Allocator, Layout};
2-
use core::iter::IntoIterator;
2+
use core::assert_eq;
3+
use core::iter::{ExactSizeIterator, IntoIterator};
34
use core::ptr::NonNull;
45
use std::alloc::System;
56
use std::assert_matches::assert_matches;
@@ -1035,6 +1036,13 @@ fn test_into_iter_clone() {
10351036
assert_eq!(it.next(), None);
10361037
}
10371038

1039+
#[test]
1040+
fn test_into_iter_default() {
1041+
let iter: IntoIter<u8> = Default::default();
1042+
assert_eq!(iter.len(), 0);
1043+
assert_eq!(iter.as_slice(), &[]);
1044+
}
1045+
10381046
#[test]
10391047
fn test_into_iter_leak() {
10401048
static mut DROPS: i32 = 0;

library/core/src/iter/adapters/chain.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ where
282282
{
283283
}
284284

285+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
286+
impl<A, B> Default for Chain<A, B>
287+
where
288+
A: Iterator + Default,
289+
B: Iterator + Default,
290+
{
291+
fn default() -> Self {
292+
Chain::new(Default::default(), Default::default())
293+
}
294+
}
295+
285296
#[inline]
286297
fn and_then_or_clear<T, U>(opt: &mut Option<T>, f: impl FnOnce(&mut T) -> Option<U>) -> Option<U> {
287298
let x = f(opt.as_mut()?);

library/core/src/iter/adapters/cloned.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,14 @@ where
153153
item.clone()
154154
}
155155
}
156+
157+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
158+
impl<'a, I, T: 'a> Default for Cloned<I>
159+
where
160+
I: Default + Iterator<Item = &'a T>,
161+
T: Clone,
162+
{
163+
fn default() -> Self {
164+
Self::new(Default::default())
165+
}
166+
}

library/core/src/iter/adapters/copied.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,14 @@ where
240240
}
241241
}
242242
}
243+
244+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
245+
impl<'a, I, T: 'a> Default for Copied<I>
246+
where
247+
I: Default + Iterator<Item = &'a T>,
248+
T: Copy,
249+
{
250+
fn default() -> Self {
251+
Self::new(Default::default())
252+
}
253+
}

library/core/src/iter/adapters/enumerate.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,13 @@ where
264264

265265
#[unstable(issue = "none", feature = "inplace_iteration")]
266266
unsafe impl<I: InPlaceIterable> InPlaceIterable for Enumerate<I> {}
267+
268+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
269+
impl<I> Default for Enumerate<I>
270+
where
271+
I: Iterator + Default,
272+
{
273+
fn default() -> Self {
274+
Enumerate::new(Default::default())
275+
}
276+
}

library/core/src/iter/adapters/flatten.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,17 @@ where
302302
{
303303
}
304304

305+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
306+
impl<I> Default for Flatten<I>
307+
where
308+
I: Iterator + Default,
309+
<I as Iterator>::Item: IntoIterator,
310+
{
311+
fn default() -> Self {
312+
Flatten::new(Default::default())
313+
}
314+
}
315+
305316
/// Real logic of both `Flatten` and `FlatMap` which simply delegate to
306317
/// this type.
307318
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)