Implement ExactSizeIterator for remaining core Iterators where applicable.
Specifically: * Peekable * ByRef * Skip * Take * Fuse
This commit is contained in:
parent
29bd9a06ef
commit
8ea6f78cf6
@ -1087,6 +1087,12 @@ impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterat
|
||||
fn next_back(&mut self) -> Option<<I as Iterator>::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<A> {
|
||||
@ -1790,6 +1796,16 @@ fn size_hint(&self) -> (uint, Option<uint>) {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T, I> ExactSizeIterator for Peekable<T, I> where I: ExactSizeIterator<Item = T> {
|
||||
#[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<T, I> Peekable<T, I> where I: Iterator<Item=T> {
|
||||
/// Return a reference to the next element of the iterator with out advancing it,
|
||||
@ -1982,6 +1998,12 @@ fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<I> ExactSizeIterator for Skip<I> 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 @@ fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<I> ExactSizeIterator for Take<I> 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 @@ fn idx(&mut self, index: uint) -> Option<<I as Iterator>::Item> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator {
|
||||
#[inline]
|
||||
fn len(&self) -> uint { self.iter.len() }
|
||||
}
|
||||
|
||||
impl<I> Fuse<I> {
|
||||
/// 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.
|
||||
|
Loading…
Reference in New Issue
Block a user