@@ -18,7 +18,7 @@ implementing the `Iterator` trait.
18
18
*/
19
19
20
20
use cmp;
21
- use num:: { Zero , One } ;
21
+ use num:: { Zero , One , Saturating } ;
22
22
use option:: { Option , Some , None } ;
23
23
use ops:: { Add , Mul } ;
24
24
use cmp:: Ord ;
@@ -863,15 +863,10 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for Chain<T, U> {
863
863
let ( a_lower, a_upper) = self . a . size_hint ( ) ;
864
864
let ( b_lower, b_upper) = self . b . size_hint ( ) ;
865
865
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) ;
871
867
872
868
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) ) ,
875
870
_ => None
876
871
} ;
877
872
@@ -895,12 +890,7 @@ for Chain<T, U> {
895
890
#[ inline]
896
891
fn indexable ( & self ) -> uint {
897
892
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)
904
894
}
905
895
906
896
#[ inline]
@@ -1252,11 +1242,10 @@ impl<A, T: Iterator<A>> Iterator<A> for Skip<T> {
1252
1242
fn size_hint ( & self ) -> ( uint , Option < uint > ) {
1253
1243
let ( lower, upper) = self . iter . size_hint ( ) ;
1254
1244
1255
- let lower = if lower >= self . n { lower - self . n } else { 0 } ;
1245
+ let lower = lower. saturating_sub ( self . n ) ;
1256
1246
1257
1247
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 ) ) ,
1260
1249
None => None
1261
1250
} ;
1262
1251
@@ -1267,12 +1256,7 @@ impl<A, T: Iterator<A>> Iterator<A> for Skip<T> {
1267
1256
impl < A , T : RandomAccessIterator < A > > RandomAccessIterator < A > for Skip < T > {
1268
1257
#[ inline]
1269
1258
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 )
1276
1260
}
1277
1261
1278
1262
#[ inline]
@@ -1389,9 +1373,10 @@ impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
1389
1373
fn size_hint ( & self ) -> ( uint , Option < uint > ) {
1390
1374
let ( flo, fhi) = self . frontiter . map_default ( ( 0 , Some ( 0 ) ) , |it| it. size_hint ( ) ) ;
1391
1375
let ( blo, bhi) = self . backiter . map_default ( ( 0 , Some ( 0 ) ) , |it| it. size_hint ( ) ) ;
1376
+ let lo = flo. saturating_add ( blo) ;
1392
1377
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 )
1395
1380
}
1396
1381
}
1397
1382
}
0 commit comments