diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs index 06c29b47bf921..5506ed3fcd599 100644 --- a/src/libcore/iter/mod.rs +++ b/src/libcore/iter/mod.rs @@ -409,7 +409,7 @@ impl LoopState { /// /// [`rev`]: trait.Iterator.html#method.rev /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Rev { @@ -506,7 +506,7 @@ unsafe impl TrustedLen for Rev /// [`Iterator`]: trait.Iterator.html #[stable(feature = "iter_cloned", since = "1.1.0")] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] pub struct Cloned { it: I, } @@ -614,7 +614,7 @@ unsafe impl<'a, I, T: 'a> TrustedLen for Cloned /// /// [`cycle`]: trait.Iterator.html#method.cycle /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Cycle { @@ -659,7 +659,7 @@ impl FusedIterator for Cycle where I: Clone + Iterator {} #[unstable(feature = "iterator_step_by", reason = "unstable replacement of Range::step_by", issue = "27741")] -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] pub struct StepBy { iter: I, step: usize, @@ -709,7 +709,7 @@ impl ExactSizeIterator for StepBy where I: ExactSizeIterator {} /// /// [`chain`]: trait.Iterator.html#method.chain /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Chain { @@ -731,7 +731,7 @@ pub struct Chain { // // The fourth state (neither iterator is remaining) only occurs after Chain has // returned None once, so we don't need to store this state. -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] enum ChainState { // both front and back iterator are remaining Both, @@ -960,7 +960,7 @@ unsafe impl TrustedLen for Chain /// /// [`zip`]: trait.Iterator.html#method.zip /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Zip { @@ -1227,7 +1227,7 @@ unsafe impl TrustedLen for Zip /// ``` #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] +#[derive(Copy, Clone)] pub struct Map { iter: I, f: F, @@ -1338,7 +1338,7 @@ unsafe impl TrustedRandomAccess for Map /// [`Iterator`]: trait.Iterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] +#[derive(Copy, Clone)] pub struct Filter { iter: I, predicate: P, @@ -1470,7 +1470,7 @@ impl FusedIterator for Filter /// [`Iterator`]: trait.Iterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] +#[derive(Copy, Clone)] pub struct FilterMap { iter: I, f: F, @@ -1739,7 +1739,7 @@ unsafe impl TrustedLen for Enumerate /// /// [`peekable`]: trait.Iterator.html#method.peekable /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Peekable { @@ -1748,6 +1748,27 @@ pub struct Peekable { peeked: Option>, } +#[stable(feature = "rust1", since = "1.25.0")] +impl Copy for Peekable +where + I: Iterator + Copy, + I::Item: Copy, +{} + +#[stable(feature = "rust1", since = "1.25.0")] +impl Clone for Peekable +where + I: Iterator + Clone, + I::Item: Clone, +{ + fn clone(&self) -> Self { + Peekable { + iter: self.iter.clone(), + peeked: self.peeked.clone(), + } + } +} + // Peekable must remember if a None has been seen in the `.peek()` method. // It ensures that `.peek(); .peek();` or `.peek(); .next();` only advances the // underlying iterator at most once. This does not by itself make the iterator @@ -1906,7 +1927,7 @@ impl Peekable { /// [`Iterator`]: trait.Iterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] +#[derive(Copy, Clone)] pub struct SkipWhile { iter: I, flag: bool, @@ -1989,7 +2010,7 @@ impl FusedIterator for SkipWhile /// [`Iterator`]: trait.Iterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] +#[derive(Copy, Clone)] pub struct TakeWhile { iter: I, flag: bool, @@ -2066,7 +2087,7 @@ impl FusedIterator for TakeWhile /// /// [`skip`]: trait.Iterator.html#method.skip /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Skip { @@ -2204,7 +2225,7 @@ impl FusedIterator for Skip where I: FusedIterator {} /// /// [`take`]: trait.Iterator.html#method.take /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Take { @@ -2287,7 +2308,7 @@ impl FusedIterator for Take where I: FusedIterator {} /// [`Iterator`]: trait.Iterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] +#[derive(Copy, Clone)] pub struct Scan { iter: I, f: F, @@ -2347,7 +2368,6 @@ impl Iterator for Scan where /// [`Iterator`]: trait.Iterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] pub struct FlatMap { iter: I, f: F, @@ -2355,6 +2375,33 @@ pub struct FlatMap { backiter: Option, } +#[stable(feature = "rust1", since = "1.25.0")] +impl Copy for FlatMap +where + I: Copy, + F: Copy, + U: IntoIterator, + U::IntoIter: Copy, +{} + +#[stable(feature = "rust1", since = "1.25.0")] +impl Clone for FlatMap +where + I: Clone, + F: Clone, + U: IntoIterator, + U::IntoIter: Clone, +{ + fn clone(&self) -> Self { + FlatMap { + iter: self.iter.clone(), + f: self.f.clone(), + frontiter: self.frontiter.clone(), + backiter: self.backiter.clone(), + } + } +} + #[stable(feature = "core_impl_debug", since = "1.9.0")] impl fmt::Debug for FlatMap where U::IntoIter: fmt::Debug @@ -2513,7 +2560,7 @@ impl FusedIterator for FlatMap /// /// [`fuse`]: trait.Iterator.html#method.fuse /// [`Iterator`]: trait.Iterator.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Fuse { @@ -2740,7 +2787,7 @@ impl ExactSizeIterator for Fuse where I: ExactSizeIterator { /// [`Iterator`]: trait.Iterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] +#[derive(Copy, Clone)] pub struct Inspect { iter: I, f: F, diff --git a/src/libcore/iter/sources.rs b/src/libcore/iter/sources.rs index b405f35d5e4db..f38276185fdc2 100644 --- a/src/libcore/iter/sources.rs +++ b/src/libcore/iter/sources.rs @@ -19,7 +19,7 @@ use super::{FusedIterator, TrustedLen}; /// This `struct` is created by the [`repeat`] function. See its documentation for more. /// /// [`repeat`]: fn.repeat.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct Repeat { element: A @@ -144,6 +144,9 @@ unsafe impl TrustedLen for Empty {} #[unstable(feature = "fused", issue = "35602")] impl FusedIterator for Empty {} +#[stable(feature = "iter_empty", since = "1.25.0")] +impl Copy for Empty {} + // not #[derive] because that adds a Clone bound on T, // which isn't necessary. #[stable(feature = "iter_empty", since = "1.2.0")] @@ -186,7 +189,7 @@ pub fn empty() -> Empty { /// This `struct` is created by the [`once`] function. See its documentation for more. /// /// [`once`]: fn.once.html -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[stable(feature = "iter_once", since = "1.2.0")] pub struct Once { inner: ::option::IntoIter diff --git a/src/libcore/option.rs b/src/libcore/option.rs index d8f3ec38cf38c..81f9803e1ddbe 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -960,7 +960,7 @@ impl From for Option { // The Option Iterators ///////////////////////////////////////////////////////////////////////////// -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] struct Item { opt: Option } @@ -1031,6 +1031,9 @@ impl<'a, A> FusedIterator for Iter<'a, A> {} #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl<'a, A> TrustedLen for Iter<'a, A> {} +#[stable(feature = "rust1", since = "1.25.0")] +impl<'a, A> Copy for Iter<'a, A> {} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, A> Clone for Iter<'a, A> { fn clone(&self) -> Iter<'a, A> { @@ -1084,7 +1087,7 @@ unsafe impl<'a, A> TrustedLen for IterMut<'a, A> {} /// [`Option`]: enum.Option.html /// [`Some`]: enum.Option.html#variant.Some /// [`Option::into_iter`]: enum.Option.html#method.into_iter -#[derive(Clone, Debug)] +#[derive(Copy, Clone, Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct IntoIter { inner: Item }