diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index ffe49a25a0d2f..8cbdda758697b 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -171,10 +171,10 @@ pub trait Iterator { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn chain(self, other: U) -> Chain where - Self: Sized, U: Iterator, + fn chain(self, other: U) -> Chain where + Self: Sized, U: IntoIterator, { - Chain{a: self, b: other, flag: false} + Chain{a: self, b: other.into_iter(), flag: false} } /// Creates an iterator that iterates over both this and the specified @@ -207,8 +207,10 @@ pub trait Iterator { /// both produce the same output. #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn zip(self, other: U) -> Zip where Self: Sized { - Zip{a: self, b: other} + fn zip(self, other: U) -> Zip where + Self: Sized, U: IntoIterator + { + Zip{a: self, b: other.into_iter()} } /// Creates a new iterator that will apply the specified function to each @@ -443,7 +445,7 @@ pub trait Iterator { #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn flat_map(self, f: F) -> FlatMap - where Self: Sized, U: Iterator, F: FnMut(Self::Item) -> U, + where Self: Sized, U: IntoIterator, F: FnMut(Self::Item) -> U, { FlatMap{iter: self, f: f, frontiter: None, backiter: None } } @@ -933,7 +935,7 @@ pub trait Iterator { /// `std::usize::MAX` elements of the original iterator. #[inline] #[stable(feature = "rust1", since = "1.0.0")] - fn rev(self) -> Rev where Self: Sized { + fn rev(self) -> Rev where Self: Sized + DoubleEndedIterator { Rev{iter: self} } @@ -2093,15 +2095,15 @@ impl Iterator for Scan where #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] -pub struct FlatMap { +pub struct FlatMap { iter: I, f: F, - frontiter: Option, - backiter: Option, + frontiter: Option, + backiter: Option, } #[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for FlatMap +impl Iterator for FlatMap where F: FnMut(I::Item) -> U, { type Item = U::Item; @@ -2110,13 +2112,13 @@ impl Iterator for FlatMap fn next(&mut self) -> Option { loop { if let Some(ref mut inner) = self.frontiter { - for x in inner.by_ref() { + if let Some(x) = inner.by_ref().next() { return Some(x) } } match self.iter.next().map(|x| (self.f)(x)) { None => return self.backiter.as_mut().and_then(|it| it.next()), - next => self.frontiter = next, + next => self.frontiter = next.map(IntoIterator::into_iter), } } } @@ -2134,22 +2136,22 @@ impl Iterator for FlatMap } #[stable(feature = "rust1", since = "1.0.0")] -impl DoubleEndedIterator - for FlatMap - where F: FnMut(I::Item) -> U +impl DoubleEndedIterator for FlatMap where + F: FnMut(I::Item) -> U, + U: IntoIterator, + U::IntoIter: DoubleEndedIterator { #[inline] fn next_back(&mut self) -> Option { loop { if let Some(ref mut inner) = self.backiter { - match inner.next_back() { - None => (), - y => return y + if let Some(y) = inner.next_back() { + return Some(y) } } match self.iter.next_back().map(|x| (self.f)(x)) { None => return self.frontiter.as_mut().and_then(|it| it.next_back()), - next => self.backiter = next, + next => self.backiter = next.map(IntoIterator::into_iter), } } }