@@ -106,9 +106,15 @@ pub trait Iterator {
106
106
107
107
/// Counts the number of elements in this iterator.
108
108
///
109
+ /// # Undefined overflow
110
+ ///
111
+ /// The method does no guarding against overflows, so counting elements of
112
+ /// an iterator with more than `usize::MAX` elements is undefined.
113
+ ///
109
114
/// # Panics
110
115
///
111
- /// Panics if the number of elements overflows a `usize`.
116
+ /// This functions might panic if the iterator has more than `usize::MAX`
117
+ /// elements.
112
118
///
113
119
/// # Examples
114
120
///
@@ -119,10 +125,8 @@ pub trait Iterator {
119
125
#[ inline]
120
126
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
121
127
fn count ( self ) -> usize where Self : Sized {
122
- self . fold ( 0 , |cnt, _| match cnt. checked_add ( 1 ) {
123
- Some ( c) => c,
124
- None => panic ! ( "overflow while counting the elements of an iterator" ) ,
125
- } )
128
+ // Might overflow.
129
+ self . fold ( 0 , |cnt, _| cnt + 1 )
126
130
}
127
131
128
132
/// Loops through the entire iterator, returning the last element.
@@ -156,10 +160,8 @@ pub trait Iterator {
156
160
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
157
161
fn nth ( & mut self , mut n : usize ) -> Option < Self :: Item > where Self : Sized {
158
162
for x in self . by_ref ( ) {
159
- n = match n. checked_sub ( 1 ) {
160
- Some ( nn) => nn,
161
- None => return Some ( x) ,
162
- }
163
+ if n == 0 { return Some ( x) }
164
+ n -= 1 ;
163
165
}
164
166
None
165
167
}
@@ -693,10 +695,16 @@ pub trait Iterator {
693
695
///
694
696
/// Does not consume the iterator past the first found element.
695
697
///
698
+ /// # Undefined overflow
699
+ ///
700
+ /// The method does no guarding against overflows, so when there are more
701
+ /// than `usize::MAX` non-matching elements, the overflow behaviour is
702
+ /// undefined.
703
+ ///
696
704
/// # Panics
697
705
///
698
- /// Panics if the number of elements overflows a `usize` and no matching
699
- /// element was found until that point .
706
+ /// This functions might panic if the iterator has more than `usize::MAX`
707
+ /// non-matching elements .
700
708
///
701
709
/// # Examples
702
710
///
@@ -712,15 +720,11 @@ pub trait Iterator {
712
720
Self : Sized ,
713
721
P : FnMut ( Self :: Item ) -> bool ,
714
722
{
715
- let mut i = 0 ;
716
- for x in self . by_ref ( ) {
723
+ // `enumerate` might overflow.
724
+ for ( i , x ) in self . by_ref ( ) . enumerate ( ) {
717
725
if predicate ( x) {
718
726
return Some ( i) ;
719
727
}
720
- i = match i. checked_add ( 1 ) {
721
- Some ( ii) => ii,
722
- None => panic ! ( "overflow while getting the position of an element in an iterator" ) ,
723
- }
724
728
}
725
729
None
726
730
}
@@ -1797,7 +1801,6 @@ pub struct Enumerate<I> {
1797
1801
impl < I > Iterator for Enumerate < I > where I : Iterator {
1798
1802
type Item = ( usize , <I as Iterator >:: Item ) ;
1799
1803
1800
-
1801
1804
/// # Undefined overflow
1802
1805
///
1803
1806
/// The method does no guarding against overflows, so enumerating more than
0 commit comments