diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 767828408be86..dccc688f90342 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -65,7 +65,7 @@ use default::Default; use marker; use mem; use num::{ToPrimitive, Int}; -use ops::{Add, Deref, FnMut}; +use ops::{Add, Deref, Fn, FnMut}; use option::Option; use option::Option::{Some, None}; use marker::Sized; @@ -537,7 +537,7 @@ pub trait IteratorExt: Iterator + Sized { fn inspect(self, f: F) -> Inspect where F: FnMut(&Self::Item), { - Inspect{iter: self, f: f} + Inspect{iter: self, f: DoInspect{f: f}} } /// Creates a wrapper around a mutable reference to the iterator. @@ -983,7 +983,7 @@ pub trait IteratorExt: Iterator + Sized { Self::Item: Deref, ::Target: Clone, { - Cloned { it: self } + Cloned { iter: self, f: DoClone } } /// Repeats an iterator endlessly @@ -1087,10 +1087,6 @@ impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I {} #[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for Enumerate where I: ExactSizeIterator {} #[stable(feature = "rust1", since = "1.0.0")] -impl ExactSizeIterator for Inspect where - F: FnMut(&I::Item), -{} -#[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for Rev where I: ExactSizeIterator + DoubleEndedIterator {} #[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for Map where @@ -1273,60 +1269,21 @@ impl MinMaxResult { /// An iterator that clones the elements of an underlying iterator #[unstable(feature = "core", reason = "recent addition")] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -#[derive(Clone)] -pub struct Cloned { - it: I, -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Cloned where - T: Clone, - D: Deref, - I: Iterator, -{ - type Item = T; - - fn next(&mut self) -> Option { - self.it.next().cloned() - } - - fn size_hint(&self) -> (usize, Option) { - self.it.size_hint() - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl DoubleEndedIterator for Cloned where - T: Clone, - D: Deref, - I: DoubleEndedIterator, -{ - fn next_back(&mut self) -> Option { - self.it.next_back().cloned() - } -} +pub type Cloned = Map; -#[stable(feature = "rust1", since = "1.0.0")] -impl ExactSizeIterator for Cloned where - T: Clone, - D: Deref, - I: ExactSizeIterator, -{} +/// clone an element +#[derive(Clone)] +pub struct DoClone; -#[unstable(feature = "core", reason = "trait is experimental")] -impl RandomAccessIterator for Cloned where - T: Clone, - D: Deref, - I: RandomAccessIterator +#[unstable(feature = "core", reason = "recent addition")] +impl Fn<(D,)> for DoClone where + D: Deref, + ::Target: Clone, { - #[inline] - fn indexable(&self) -> usize { - self.it.indexable() - } + type Output = ::Target; - #[inline] - fn idx(&mut self, index: usize) -> Option { - self.it.idx(index).cloned() + extern "rust-call" fn call(&self, (a,): (D,)) -> ::Target { + a.deref().clone() } } @@ -2231,64 +2188,20 @@ impl Fuse { /// element before yielding it. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] -#[derive(Clone)] -pub struct Inspect { - iter: I, - f: F, -} - -impl Inspect where F: FnMut(&I::Item) { - #[inline] - fn do_inspect(&mut self, elt: Option) -> Option { - match elt { - Some(ref a) => (self.f)(a), - None => () - } - - elt - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl Iterator for Inspect where F: FnMut(&I::Item) { - type Item = I::Item; - - #[inline] - fn next(&mut self) -> Option { - let next = self.iter.next(); - self.do_inspect(next) - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - self.iter.size_hint() - } -} +pub type Inspect = Map>; -#[stable(feature = "rust1", since = "1.0.0")] -impl DoubleEndedIterator for Inspect - where F: FnMut(&I::Item), -{ - #[inline] - fn next_back(&mut self) -> Option { - let next = self.iter.next_back(); - self.do_inspect(next) - } -} +/// Inspect a element and return it +#[derive(Clone)] +pub struct DoInspect { f: F, } -#[unstable(feature = "core", reason = "trait is experimental")] -impl RandomAccessIterator for Inspect - where F: FnMut(&I::Item), +#[unstable(feature = "core", reason = "recent addition")] +impl FnMut<(D,)> for DoInspect { - #[inline] - fn indexable(&self) -> usize { - self.iter.indexable() - } + type Output = D; - #[inline] - fn idx(&mut self, index: usize) -> Option { - let element = self.iter.idx(index); - self.do_inspect(element) + extern "rust-call" fn call_mut(&mut self, (a,): (D,)) -> D { + (self.f)(&a); + a } }