11use crate :: iter:: adapters:: { zip:: try_get_unchecked, SourceIter , TrustedRandomAccess } ;
22use crate :: iter:: { FusedIterator , InPlaceIterable , TrustedLen } ;
3- use crate :: ops:: { Add , AddAssign , Try } ;
3+ use crate :: ops:: Try ;
44
55/// An iterator that yields the current count and the element during iteration.
66///
@@ -39,11 +39,11 @@ where
3939 ///
4040 /// Might panic if the index of the element overflows a `usize`.
4141 #[ inline]
42+ #[ rustc_inherit_overflow_checks]
4243 fn next ( & mut self ) -> Option < ( usize , <I as Iterator >:: Item ) > {
4344 let a = self . iter . next ( ) ?;
4445 let i = self . count ;
45- // Possible undefined overflow.
46- AddAssign :: add_assign ( & mut self . count , 1 ) ;
46+ self . count += 1 ;
4747 Some ( ( i, a) )
4848 }
4949
@@ -53,11 +53,11 @@ where
5353 }
5454
5555 #[ inline]
56+ #[ rustc_inherit_overflow_checks]
5657 fn nth ( & mut self , n : usize ) -> Option < ( usize , I :: Item ) > {
5758 let a = self . iter . nth ( n) ?;
58- // Possible undefined overflow.
59- let i = Add :: add ( self . count , n) ;
60- self . count = Add :: add ( i, 1 ) ;
59+ let i = self . count + n;
60+ self . count = i + 1 ;
6161 Some ( ( i, a) )
6262 }
6363
@@ -78,10 +78,10 @@ where
7878 count : & ' a mut usize ,
7979 mut fold : impl FnMut ( Acc , ( usize , T ) ) -> R + ' a ,
8080 ) -> impl FnMut ( Acc , T ) -> R + ' a {
81+ #[ rustc_inherit_overflow_checks]
8182 move |acc, item| {
8283 let acc = fold ( acc, ( * count, item) ) ;
83- // Possible undefined overflow.
84- AddAssign :: add_assign ( count, 1 ) ;
84+ * count += 1 ;
8585 acc
8686 }
8787 }
@@ -99,25 +99,26 @@ where
9999 mut count : usize ,
100100 mut fold : impl FnMut ( Acc , ( usize , T ) ) -> Acc ,
101101 ) -> impl FnMut ( Acc , T ) -> Acc {
102+ #[ rustc_inherit_overflow_checks]
102103 move |acc, item| {
103104 let acc = fold ( acc, ( count, item) ) ;
104- // Possible undefined overflow.
105- AddAssign :: add_assign ( & mut count, 1 ) ;
105+ count += 1 ;
106106 acc
107107 }
108108 }
109109
110110 self . iter . fold ( init, enumerate ( self . count , fold) )
111111 }
112112
113+ #[ rustc_inherit_overflow_checks]
113114 unsafe fn __iterator_get_unchecked ( & mut self , idx : usize ) -> <Self as Iterator >:: Item
114115 where
115116 Self : TrustedRandomAccess ,
116117 {
117118 // SAFETY: the caller must uphold the contract for
118119 // `Iterator::__iterator_get_unchecked`.
119120 let value = unsafe { try_get_unchecked ( & mut self . iter , idx) } ;
120- ( Add :: add ( self . count , idx) , value)
121+ ( self . count + idx, value)
121122 }
122123}
123124
0 commit comments