Skip to content

Commit 8ea6f78

Browse files
committed
Implement ExactSizeIterator for remaining core Iterators where applicable.
Specifically: * Peekable * ByRef * Skip * Take * Fuse
1 parent 29bd9a0 commit 8ea6f78

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libcore/iter.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,12 @@ impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterat
10871087
fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next_back() }
10881088
}
10891089

1090+
#[stable]
1091+
impl<'a, I> ExactSizeIterator for ByRef<'a, I> where I: 'a + ExactSizeIterator {
1092+
#[inline]
1093+
fn len(&self) -> uint { self.iter.len() }
1094+
}
1095+
10901096
/// A trait for iterators over elements which can be added together
10911097
#[unstable = "needs to be re-evaluated as part of numerics reform"]
10921098
pub trait AdditiveIterator<A> {
@@ -1790,6 +1796,16 @@ impl<T, I> Iterator for Peekable<T, I> where I: Iterator<Item=T> {
17901796
}
17911797
}
17921798

1799+
#[stable]
1800+
impl<T, I> ExactSizeIterator for Peekable<T, I> where I: ExactSizeIterator<Item = T> {
1801+
#[inline]
1802+
fn len(&self) -> usize {
1803+
// This is guarenteed to not overflow because `len()` must have been able to return a valid
1804+
// value before we peeked.
1805+
self.iter.len() + if self.peeked.is_some() { 1 } else { 0 }
1806+
}
1807+
}
1808+
17931809
#[stable]
17941810
impl<T, I> Peekable<T, I> where I: Iterator<Item=T> {
17951811
/// Return a reference to the next element of the iterator with out advancing it,
@@ -1982,6 +1998,12 @@ impl<I> RandomAccessIterator for Skip<I> where I: RandomAccessIterator{
19821998
}
19831999
}
19842000

2001+
#[stable]
2002+
impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator {
2003+
#[inline]
2004+
fn len(&self) -> uint { self.iter.len().saturating_sub(self.n) }
2005+
}
2006+
19852007
/// An iterator that only iterates over the first `n` iterations of `iter`.
19862008
#[derive(Clone)]
19872009
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
@@ -2037,6 +2059,12 @@ impl<I> RandomAccessIterator for Take<I> where I: RandomAccessIterator{
20372059
}
20382060
}
20392061

2062+
#[stable]
2063+
impl<I> ExactSizeIterator for Take<I> where I: ExactSizeIterator {
2064+
#[inline]
2065+
fn len(&self) -> uint { cmp::min(self.iter.len(), self.n) }
2066+
}
2067+
20402068

20412069
/// An iterator to maintain state while iterating another iterator
20422070
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
@@ -2246,6 +2274,12 @@ impl<I> RandomAccessIterator for Fuse<I> where I: RandomAccessIterator {
22462274
}
22472275
}
22482276

2277+
#[stable]
2278+
impl<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator {
2279+
#[inline]
2280+
fn len(&self) -> uint { self.iter.len() }
2281+
}
2282+
22492283
impl<I> Fuse<I> {
22502284
/// Resets the fuse such that the next call to .next() or .next_back() will
22512285
/// call the underlying iterator again even if it previously returned None.

0 commit comments

Comments
 (0)