diff options
| author | The 8472 <git@infinite-source.de> | 2022-07-30 01:48:16 +0200 |
|---|---|---|
| committer | The 8472 <git@infinite-source.de> | 2023-02-28 21:00:00 +0100 |
| commit | 05c7330ca03650bbcb6a55f5fa490b3bb03c1940 (patch) | |
| tree | b7ace4e112ca9e379342f60cd877fbb40211b400 /library/core/src | |
| parent | 31f858d9a511f24fedb8ed997b28304fec809630 (diff) | |
| download | rust-05c7330ca03650bbcb6a55f5fa490b3bb03c1940.tar.gz rust-05c7330ca03650bbcb6a55f5fa490b3bb03c1940.zip | |
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.
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/iter/adapters/chain.rs | 11 | ||||
| -rw-r--r-- | library/core/src/iter/adapters/cloned.rs | 11 | ||||
| -rw-r--r-- | library/core/src/iter/adapters/copied.rs | 11 | ||||
| -rw-r--r-- | library/core/src/iter/adapters/enumerate.rs | 10 | ||||
| -rw-r--r-- | library/core/src/iter/adapters/flatten.rs | 11 | ||||
| -rw-r--r-- | library/core/src/iter/adapters/fuse.rs | 7 | ||||
| -rw-r--r-- | library/core/src/iter/adapters/rev.rs | 10 | ||||
| -rw-r--r-- | library/core/src/slice/iter/macros.rs | 7 |
8 files changed, 78 insertions, 0 deletions
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<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()?); 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<I> +where + I: Default + Iterator<Item = &'a T>, + 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<I> +where + I: Default + Iterator<Item = &'a T>, + 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<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()) + } +} 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<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)] 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<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 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<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()) + } +} 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<T> Default for $name<'_, T> { + fn default() -> Self { + (& $( $mut_ )? []).into_iter() + } + } } } |
