69
69
default fn next ( & mut self ) -> Option < Self :: Item > {
70
70
let a = self . iter . next ( ) ?;
71
71
let i = self . count ;
72
- // Possible undefined overflow.
72
+ // Possible undefined overflow. By directly calling the trait method instead of using the
73
+ // `+=` operator the decision about overflow checking is delayed to the crate that does code
74
+ // generation, even if overflow checks are disabled for the current crate. This is
75
+ // especially useful because overflow checks are usually disabled for the standard library.
73
76
AddAssign :: add_assign ( & mut self . count , 1 ) ;
74
77
Some ( ( i, a) )
75
78
}
82
85
// SAFETY: the caller must uphold the contract for
83
86
// `Iterator::__iterator_get_unchecked`.
84
87
let value = unsafe { try_get_unchecked ( & mut self . iter , idx) } ;
88
+ // See comment in `next()` for the reason why `Add::add()` is used here instead of `+`.
85
89
( Add :: add ( self . count , idx) , value)
86
90
}
87
91
@@ -123,7 +127,8 @@ where
123
127
intrinsics:: assume ( self . count < self . len ) ;
124
128
}
125
129
let i = self . count ;
126
- // Possible undefined overflow.
130
+ // See comment in `next()` of the default implementation for the reason why
131
+ // `AddAssign::add_assign()` is used here instead of `+=`.
127
132
AddAssign :: add_assign ( & mut self . count , 1 ) ;
128
133
Some ( ( i, a) )
129
134
}
@@ -136,6 +141,7 @@ where
136
141
// SAFETY: the caller must uphold the contract for
137
142
// `Iterator::__iterator_get_unchecked`.
138
143
let value = unsafe { try_get_unchecked ( & mut self . iter , idx) } ;
144
+ // See comment in `next()` for the reason why `Add::add()` is used here instead of `+`.
139
145
let idx = Add :: add ( self . count , idx) ;
140
146
// SAFETY: There must be fewer than `self.len` items because of `TrustedLen`'s API contract
141
147
unsafe {
0 commit comments