*/
use cmp;
-use num::{Zero, One, Integer, Saturating};
+use num::{Zero, One, Integer, CheckedAdd, Saturating};
use option::{Option, Some, None};
use ops::{Add, Mul, Sub};
use cmp::Ord;
let lower = a_lower.saturating_add(b_lower);
let upper = match (a_upper, b_upper) {
- (Some(x), Some(y)) => Some(x.saturating_add(y)),
+ (Some(x), Some(y)) => x.checked_add(&y),
_ => None
};
if self.peeked.is_some() { self.peeked.take() }
else { self.iter.next() }
}
+
+ #[inline]
+ fn size_hint(&self) -> (uint, Option<uint>) {
+ let (lo, hi) = self.iter.size_hint();
+ if self.peeked.is_some() {
+ let lo = lo.saturating_add(1);
+ let hi = match hi {
+ Some(x) => x.checked_add(&1),
+ None => None
+ };
+ (lo, hi)
+ } else {
+ (lo, hi)
+ }
+ }
}
impl<'self, A, T: Iterator<A>> Peekable<A, T> {
/// or None if the iterator is exhausted.
#[inline]
pub fn peek(&'self mut self) -> Option<&'self A> {
+ if self.peeked.is_none() {
+ self.peeked = self.iter.next();
+ }
match self.peeked {
Some(ref value) => Some(value),
- None => {
- self.peeked = self.iter.next();
- match self.peeked {
- Some(ref value) => Some(value),
- None => None,
- }
- },
+ None => None,
}
}
}
let (blo, bhi) = self.backiter.map_default((0, Some(0)), |it| it.size_hint());
let lo = flo.saturating_add(blo);
match (self.iter.size_hint(), fhi, bhi) {
- ((0, Some(0)), Some(a), Some(b)) => (lo, Some(a.saturating_add(b))),
+ ((0, Some(0)), Some(a), Some(b)) => (lo, a.checked_add(&b)),
_ => (lo, None)
}
}
fn next(&mut self) -> Option<A> {
(self.f)(&mut self.state)
}
+
+ #[inline]
+ fn size_hint(&self) -> (uint, Option<uint>) {
+ // no possible known bounds at this point
+ (0, None)
+ }
}
/// An infinite iterator starting at `start` and advancing by `step` with each
None
}
}
+
+ // FIXME: #8606 Implement size_hint() on Range
+ // Blocked on #8605 Need numeric trait for converting to `Option<uint>`
}
impl<A: Sub<A, A> + Integer + Ord + Clone> DoubleEndedIterator<A> for Range<A> {