From 8ea6f78cf6779a933e2dd859efee995932ef1371 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 20 Jan 2015 17:41:58 -0500 Subject: Implement ExactSizeIterator for remaining core Iterators where applicable. Specifically: * Peekable * ByRef * Skip * Take * Fuse --- src/libcore/iter.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 0005db36c27..cfbccef2a72 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -1087,6 +1087,12 @@ impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterat fn next_back(&mut self) -> Option<::Item> { self.iter.next_back() } } +#[stable] +impl<'a, I> ExactSizeIterator for ByRef<'a, I> where I: 'a + ExactSizeIterator { + #[inline] + fn len(&self) -> uint { self.iter.len() } +} + /// A trait for iterators over elements which can be added together #[unstable = "needs to be re-evaluated as part of numerics reform"] pub trait AdditiveIterator { @@ -1790,6 +1796,16 @@ impl Iterator for Peekable where I: Iterator { } } +#[stable] +impl ExactSizeIterator for Peekable where I: ExactSizeIterator { + #[inline] + fn len(&self) -> usize { + // This is guarenteed to not overflow because `len()` must have been able to return a valid + // value before we peeked. + self.iter.len() + if self.peeked.is_some() { 1 } else { 0 } + } +} + #[stable] impl Peekable where I: Iterator { /// Return a reference to the next element of the iterator with out advancing it, @@ -1982,6 +1998,12 @@ impl RandomAccessIterator for Skip where I: RandomAccessIterator{ } } +#[stable] +impl ExactSizeIterator for Skip where I: ExactSizeIterator { + #[inline] + fn len(&self) -> uint { self.iter.len().saturating_sub(self.n) } +} + /// An iterator that only iterates over the first `n` iterations of `iter`. #[derive(Clone)] #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] @@ -2037,6 +2059,12 @@ impl RandomAccessIterator for Take where I: RandomAccessIterator{ } } +#[stable] +impl ExactSizeIterator for Take where I: ExactSizeIterator { + #[inline] + fn len(&self) -> uint { cmp::min(self.iter.len(), self.n) } +} + /// An iterator to maintain state while iterating another iterator #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] @@ -2246,6 +2274,12 @@ impl RandomAccessIterator for Fuse where I: RandomAccessIterator { } } +#[stable] +impl ExactSizeIterator for Fuse where I: ExactSizeIterator { + #[inline] + fn len(&self) -> uint { self.iter.len() } +} + impl Fuse { /// Resets the fuse such that the next call to .next() or .next_back() will /// call the underlying iterator again even if it previously returned None. -- cgit 1.4.1-3-g733a5 From a13e7212025e5339133126d129e64fe8e7aff84b Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 21 Jan 2015 00:14:04 -0500 Subject: Use the default `len` implementation in ExactSizeIterator impls --- src/libcore/iter.rs | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index cfbccef2a72..e39c60a4006 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -1088,10 +1088,7 @@ impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterat } #[stable] -impl<'a, I> ExactSizeIterator for ByRef<'a, I> where I: 'a + ExactSizeIterator { - #[inline] - fn len(&self) -> uint { self.iter.len() } -} +impl<'a, I> ExactSizeIterator for ByRef<'a, I> where I: 'a + ExactSizeIterator {} /// A trait for iterators over elements which can be added together #[unstable = "needs to be re-evaluated as part of numerics reform"] @@ -1797,14 +1794,7 @@ impl Iterator for Peekable where I: Iterator { } #[stable] -impl ExactSizeIterator for Peekable where I: ExactSizeIterator { - #[inline] - fn len(&self) -> usize { - // This is guarenteed to not overflow because `len()` must have been able to return a valid - // value before we peeked. - self.iter.len() + if self.peeked.is_some() { 1 } else { 0 } - } -} +impl ExactSizeIterator for Peekable where I: ExactSizeIterator {} #[stable] impl Peekable where I: Iterator { @@ -1999,10 +1989,7 @@ impl RandomAccessIterator for Skip where I: RandomAccessIterator{ } #[stable] -impl ExactSizeIterator for Skip where I: ExactSizeIterator { - #[inline] - fn len(&self) -> uint { self.iter.len().saturating_sub(self.n) } -} +impl ExactSizeIterator for Skip where I: ExactSizeIterator {} /// An iterator that only iterates over the first `n` iterations of `iter`. #[derive(Clone)] @@ -2060,10 +2047,7 @@ impl RandomAccessIterator for Take where I: RandomAccessIterator{ } #[stable] -impl ExactSizeIterator for Take where I: ExactSizeIterator { - #[inline] - fn len(&self) -> uint { cmp::min(self.iter.len(), self.n) } -} +impl ExactSizeIterator for Take where I: ExactSizeIterator {} /// An iterator to maintain state while iterating another iterator @@ -2275,10 +2259,7 @@ impl RandomAccessIterator for Fuse where I: RandomAccessIterator { } #[stable] -impl ExactSizeIterator for Fuse where I: ExactSizeIterator { - #[inline] - fn len(&self) -> uint { self.iter.len() } -} +impl ExactSizeIterator for Fuse where I: ExactSizeIterator {} impl Fuse { /// Resets the fuse such that the next call to .next() or .next_back() will -- cgit 1.4.1-3-g733a5