@@ -106,6 +106,18 @@ pub trait Iterator {
106
106
107
107
/// Counts the number of elements in this iterator.
108
108
///
109
+ /// # Overflow Behavior
110
+ ///
111
+ /// The method does no guarding against overflows, so counting elements of
112
+ /// an iterator with more than `usize::MAX` elements either produces the
113
+ /// wrong result or panics. If debug assertions are enabled, a panic is
114
+ /// guaranteed.
115
+ ///
116
+ /// # Panics
117
+ ///
118
+ /// This functions might panic if the iterator has more than `usize::MAX`
119
+ /// elements.
120
+ ///
109
121
/// # Examples
110
122
///
111
123
/// ```
@@ -115,7 +127,8 @@ pub trait Iterator {
115
127
#[ inline]
116
128
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
117
129
fn count ( self ) -> usize where Self : Sized {
118
- self . fold ( 0 , |cnt, _x| cnt + 1 )
130
+ // Might overflow.
131
+ self . fold ( 0 , |cnt, _| cnt + 1 )
119
132
}
120
133
121
134
/// Loops through the entire iterator, returning the last element.
@@ -281,6 +294,17 @@ pub trait Iterator {
281
294
/// different sized integer, the `zip` function provides similar
282
295
/// functionality.
283
296
///
297
+ /// # Overflow Behavior
298
+ ///
299
+ /// The method does no guarding against overflows, so enumerating more than
300
+ /// `usize::MAX` elements either produces the wrong result or panics. If
301
+ /// debug assertions are enabled, a panic is guaranteed.
302
+ ///
303
+ /// # Panics
304
+ ///
305
+ /// The returned iterator might panic if the to-be-returned index would
306
+ /// overflow a `usize`.
307
+ ///
284
308
/// # Examples
285
309
///
286
310
/// ```
@@ -293,7 +317,7 @@ pub trait Iterator {
293
317
#[ inline]
294
318
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
295
319
fn enumerate ( self ) -> Enumerate < Self > where Self : Sized {
296
- Enumerate { iter : self , count : 0 }
320
+ Enumerate { iter : self , count : 0 }
297
321
}
298
322
299
323
/// Creates an iterator that has a `.peek()` method
@@ -672,6 +696,18 @@ pub trait Iterator {
672
696
///
673
697
/// Does not consume the iterator past the first found element.
674
698
///
699
+ /// # Overflow Behavior
700
+ ///
701
+ /// The method does no guarding against overflows, so if there are more
702
+ /// than `usize::MAX` non-matching elements, it either produces the wrong
703
+ /// result or panics. If debug assertions are enabled, a panic is
704
+ /// guaranteed.
705
+ ///
706
+ /// # Panics
707
+ ///
708
+ /// This functions might panic if the iterator has more than `usize::MAX`
709
+ /// non-matching elements.
710
+ ///
675
711
/// # Examples
676
712
///
677
713
/// ```
@@ -685,12 +721,11 @@ pub trait Iterator {
685
721
Self : Sized ,
686
722
P : FnMut ( Self :: Item ) -> bool ,
687
723
{
688
- let mut i = 0 ;
689
- for x in self . by_ref ( ) {
724
+ // `enumerate` might overflow.
725
+ for ( i , x ) in self . by_ref ( ) . enumerate ( ) {
690
726
if predicate ( x) {
691
727
return Some ( i) ;
692
728
}
693
- i += 1 ;
694
729
}
695
730
None
696
731
}
@@ -720,6 +755,8 @@ pub trait Iterator {
720
755
if predicate ( v) {
721
756
return Some ( i - 1 ) ;
722
757
}
758
+ // No need for an overflow check here, because `ExactSizeIterator`
759
+ // implies that the number of elements fits into a `usize`.
723
760
i -= 1 ;
724
761
}
725
762
None
@@ -1783,17 +1820,27 @@ impl<B, I: DoubleEndedIterator, F> DoubleEndedIterator for FilterMap<I, F>
1783
1820
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1784
1821
pub struct Enumerate < I > {
1785
1822
iter : I ,
1786
- count : usize
1823
+ count : usize ,
1787
1824
}
1788
1825
1789
1826
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1790
1827
impl < I > Iterator for Enumerate < I > where I : Iterator {
1791
1828
type Item = ( usize , <I as Iterator >:: Item ) ;
1792
1829
1830
+ /// # Overflow Behavior
1831
+ ///
1832
+ /// The method does no guarding against overflows, so enumerating more than
1833
+ /// `usize::MAX` elements either produces the wrong result or panics. If
1834
+ /// debug assertions are enabled, a panic is guaranteed.
1835
+ ///
1836
+ /// # Panics
1837
+ ///
1838
+ /// Might panic if the index of the element overflows a `usize`.
1793
1839
#[ inline]
1794
1840
fn next ( & mut self ) -> Option < ( usize , <I as Iterator >:: Item ) > {
1795
1841
self . iter . next ( ) . map ( |a| {
1796
1842
let ret = ( self . count , a) ;
1843
+ // Possible undefined overflow.
1797
1844
self . count += 1 ;
1798
1845
ret
1799
1846
} )
@@ -1827,6 +1874,8 @@ impl<I> DoubleEndedIterator for Enumerate<I> where
1827
1874
fn next_back ( & mut self ) -> Option < ( usize , <I as Iterator >:: Item ) > {
1828
1875
self . iter . next_back ( ) . map ( |a| {
1829
1876
let len = self . iter . len ( ) ;
1877
+ // Can safely add, `ExactSizeIterator` promises that the number of
1878
+ // elements fits into a `usize`.
1830
1879
( self . count + len, a)
1831
1880
} )
1832
1881
}
@@ -1841,6 +1890,9 @@ impl<I> RandomAccessIterator for Enumerate<I> where I: RandomAccessIterator {
1841
1890
1842
1891
#[ inline]
1843
1892
fn idx ( & mut self , index : usize ) -> Option < ( usize , <I as Iterator >:: Item ) > {
1893
+ // Can safely add, `ExactSizeIterator` (ancestor of
1894
+ // `RandomAccessIterator`) promises that the number of elements fits
1895
+ // into a `usize`.
1844
1896
self . iter . idx ( index) . map ( |a| ( self . count + index, a) )
1845
1897
}
1846
1898
}
0 commit comments