From 05c7330ca03650bbcb6a55f5fa490b3bb03c1940 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Sat, 30 Jul 2022 01:48:16 +0200 Subject: 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. --- library/core/src/iter/adapters/chain.rs | 11 +++++++++++ library/core/src/iter/adapters/cloned.rs | 11 +++++++++++ library/core/src/iter/adapters/copied.rs | 11 +++++++++++ library/core/src/iter/adapters/enumerate.rs | 10 ++++++++++ library/core/src/iter/adapters/flatten.rs | 11 +++++++++++ library/core/src/iter/adapters/fuse.rs | 7 +++++++ library/core/src/iter/adapters/rev.rs | 10 ++++++++++ library/core/src/slice/iter/macros.rs | 7 +++++++ library/core/tests/slice.rs | 10 +++++++++- 9 files changed, 87 insertions(+), 1 deletion(-) (limited to 'library/core') diff --git a/library/core/src/iter/adapters/chain.rs b/library/core/src/iter/adapters/chain.rs index 60eb3a6da3a..21fc3bc6f6f 100644 --- a/library/core/src/iter/adapters/chain.rs +++ b/library/core/src/iter/adapters/chain.rs @@ -282,6 +282,17 @@ where { } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Chain +where + A: Iterator + Default, + B: Iterator + Default, +{ + fn default() -> Self { + Chain::new(Default::default(), Default::default()) + } +} + #[inline] fn and_then_or_clear(opt: &mut Option, f: impl FnOnce(&mut T) -> Option) -> Option { let x = f(opt.as_mut()?); diff --git a/library/core/src/iter/adapters/cloned.rs b/library/core/src/iter/adapters/cloned.rs index 914ff86c1a9..d22a6e721f5 100644 --- a/library/core/src/iter/adapters/cloned.rs +++ b/library/core/src/iter/adapters/cloned.rs @@ -153,3 +153,14 @@ where item.clone() } } + +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl<'a, I, T: 'a> Default for Cloned +where + I: Default + Iterator, + T: Clone, +{ + fn default() -> Self { + Self::new(Default::default()) + } +} diff --git a/library/core/src/iter/adapters/copied.rs b/library/core/src/iter/adapters/copied.rs index 62d3afb8160..d5e579834ee 100644 --- a/library/core/src/iter/adapters/copied.rs +++ b/library/core/src/iter/adapters/copied.rs @@ -240,3 +240,14 @@ where } } } + +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl<'a, I, T: 'a> Default for Copied +where + I: Default + Iterator, + T: Copy, +{ + fn default() -> Self { + Self::new(Default::default()) + } +} diff --git a/library/core/src/iter/adapters/enumerate.rs b/library/core/src/iter/adapters/enumerate.rs index 14a12695111..0b44139a83b 100644 --- a/library/core/src/iter/adapters/enumerate.rs +++ b/library/core/src/iter/adapters/enumerate.rs @@ -264,3 +264,13 @@ where #[unstable(issue = "none", feature = "inplace_iteration")] unsafe impl InPlaceIterable for Enumerate {} + +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Enumerate +where + I: Iterator + Default, +{ + fn default() -> Self { + Enumerate::new(Default::default()) + } +} diff --git a/library/core/src/iter/adapters/flatten.rs b/library/core/src/iter/adapters/flatten.rs index b040a0ea901..c0a347a7c1d 100644 --- a/library/core/src/iter/adapters/flatten.rs +++ b/library/core/src/iter/adapters/flatten.rs @@ -302,6 +302,17 @@ where { } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Flatten +where + I: Iterator + Default, + ::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)] diff --git a/library/core/src/iter/adapters/fuse.rs b/library/core/src/iter/adapters/fuse.rs index c9314454203..87275fa3951 100644 --- a/library/core/src/iter/adapters/fuse.rs +++ b/library/core/src/iter/adapters/fuse.rs @@ -181,6 +181,13 @@ where } } +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Fuse { + 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 diff --git a/library/core/src/iter/adapters/rev.rs b/library/core/src/iter/adapters/rev.rs index 139fb7bbdd9..4ad75ec0ea2 100644 --- a/library/core/src/iter/adapters/rev.rs +++ b/library/core/src/iter/adapters/rev.rs @@ -135,3 +135,13 @@ impl FusedIterator for Rev where I: FusedIterator + DoubleEndedIterator {} #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl TrustedLen for Rev where I: TrustedLen + DoubleEndedIterator {} + +#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] +impl Default for Rev +where + I: Default + Iterator, +{ + fn default() -> Self { + Rev::new(Default::default()) + } +} diff --git a/library/core/src/slice/iter/macros.rs b/library/core/src/slice/iter/macros.rs index 89b92a7d597..57754182c4e 100644 --- a/library/core/src/slice/iter/macros.rs +++ b/library/core/src/slice/iter/macros.rs @@ -393,6 +393,13 @@ macro_rules! iterator { } } } + + #[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")] + impl Default for $name<'_, T> { + fn default() -> Self { + (& $( $mut_ )? []).into_iter() + } + } } } diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs index 39559cdbb5e..294de77a6b0 100644 --- a/library/core/tests/slice.rs +++ b/library/core/tests/slice.rs @@ -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]; -- cgit 1.4.1-3-g733a5