Skip to content

Commit 3db3ce2

Browse files
committed
Update Iterator impls to use Saturating
Replace hand-rolled saturation math with calls to Saturating. Fix one impl that didn't use saturating math.
1 parent 5f713da commit 3db3ce2

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

src/libstd/iterator.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ implementing the `Iterator` trait.
1818
*/
1919

2020
use cmp;
21-
use num::{Zero, One};
21+
use num::{Zero, One, Saturating};
2222
use option::{Option, Some, None};
2323
use ops::{Add, Mul};
2424
use cmp::Ord;
@@ -863,15 +863,10 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for Chain<T, U> {
863863
let (a_lower, a_upper) = self.a.size_hint();
864864
let (b_lower, b_upper) = self.b.size_hint();
865865

866-
let lower = if uint::max_value - a_lower < b_lower {
867-
uint::max_value
868-
} else {
869-
a_lower + b_lower
870-
};
866+
let lower = a_lower.saturating_add(b_lower);
871867

872868
let upper = match (a_upper, b_upper) {
873-
(Some(x), Some(y)) if uint::max_value - x < y => Some(uint::max_value),
874-
(Some(x), Some(y)) => Some(x + y),
869+
(Some(x), Some(y)) => Some(x.saturating_add(y)),
875870
_ => None
876871
};
877872

@@ -895,12 +890,7 @@ for Chain<T, U> {
895890
#[inline]
896891
fn indexable(&self) -> uint {
897892
let (a, b) = (self.a.indexable(), self.b.indexable());
898-
let total = a + b;
899-
if total < a || total < b {
900-
uint::max_value
901-
} else {
902-
total
903-
}
893+
a.saturating_add(b)
904894
}
905895

906896
#[inline]
@@ -1252,11 +1242,10 @@ impl<A, T: Iterator<A>> Iterator<A> for Skip<T> {
12521242
fn size_hint(&self) -> (uint, Option<uint>) {
12531243
let (lower, upper) = self.iter.size_hint();
12541244

1255-
let lower = if lower >= self.n { lower - self.n } else { 0 };
1245+
let lower = lower.saturating_sub(self.n);
12561246

12571247
let upper = match upper {
1258-
Some(x) if x >= self.n => Some(x - self.n),
1259-
Some(_) => Some(0),
1248+
Some(x) => Some(x.saturating_sub(self.n)),
12601249
None => None
12611250
};
12621251

@@ -1267,12 +1256,7 @@ impl<A, T: Iterator<A>> Iterator<A> for Skip<T> {
12671256
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Skip<T> {
12681257
#[inline]
12691258
fn indexable(&self) -> uint {
1270-
let N = self.iter.indexable();
1271-
if N < self.n {
1272-
0
1273-
} else {
1274-
N - self.n
1275-
}
1259+
self.iter.indexable().saturating_sub(self.n)
12761260
}
12771261

12781262
#[inline]
@@ -1389,9 +1373,10 @@ impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
13891373
fn size_hint(&self) -> (uint, Option<uint>) {
13901374
let (flo, fhi) = self.frontiter.map_default((0, Some(0)), |it| it.size_hint());
13911375
let (blo, bhi) = self.backiter.map_default((0, Some(0)), |it| it.size_hint());
1376+
let lo = flo.saturating_add(blo);
13921377
match (self.iter.size_hint(), fhi, bhi) {
1393-
((0, Some(0)), Some(a), Some(b)) => (flo + blo, Some(a + b)),
1394-
_ => (flo + blo, None)
1378+
((0, Some(0)), Some(a), Some(b)) => (lo, Some(a.saturating_add(b))),
1379+
_ => (lo, None)
13951380
}
13961381
}
13971382
}

0 commit comments

Comments
 (0)