@@ -227,13 +227,16 @@ where
227
227
fn next ( & mut self ) -> Option < ( A :: Item , B :: Item ) > {
228
228
if self . index < self . len {
229
229
let i = self . index ;
230
+ // since get_unchecked executes code which can panic we increment the counters beforehand
231
+ // so that the same index won't be accessed twice, as required by TrustedRandomAccess
230
232
self . index += 1 ;
231
233
// SAFETY: `i` is smaller than `self.len`, thus smaller than `self.a.len()` and `self.b.len()`
232
234
unsafe {
233
235
Some ( ( self . a . __iterator_get_unchecked ( i) , self . b . __iterator_get_unchecked ( i) ) )
234
236
}
235
237
} else if A :: MAY_HAVE_SIDE_EFFECT && self . index < self . a_len {
236
238
let i = self . index ;
239
+ // as above, increment before executing code that may panic
237
240
self . index += 1 ;
238
241
self . len += 1 ;
239
242
// match the base implementation's potential side effects
@@ -259,6 +262,8 @@ where
259
262
let end = self . index + delta;
260
263
while self . index < end {
261
264
let i = self . index ;
265
+ // since get_unchecked executes code which can panic we increment the counters beforehand
266
+ // so that the same index won't be accessed twice, as required by TrustedRandomAccess
262
267
self . index += 1 ;
263
268
if A :: MAY_HAVE_SIDE_EFFECT {
264
269
// SAFETY: the usage of `cmp::min` to calculate `delta`
@@ -295,6 +300,8 @@ where
295
300
let sz_a = self . a . size ( ) ;
296
301
if A :: MAY_HAVE_SIDE_EFFECT && sz_a > self . len {
297
302
for _ in 0 ..sz_a - self . len {
303
+ // since next_back() may panic we increment the counters beforehand
304
+ // to keep Zip's state in sync with the underlying iterator source
298
305
self . a_len -= 1 ;
299
306
self . a . next_back ( ) ;
300
307
}
@@ -309,6 +316,8 @@ where
309
316
}
310
317
}
311
318
if self . index < self . len {
319
+ // since get_unchecked executes code which can panic we increment the counters beforehand
320
+ // so that the same index won't be accessed twice, as required by TrustedRandomAccess
312
321
self . len -= 1 ;
313
322
self . a_len -= 1 ;
314
323
let i = self . len ;
0 commit comments