@@ -227,13 +227,16 @@ where
227227 fn next ( & mut self ) -> Option < ( A :: Item , B :: Item ) > {
228228 if self . index < self . len {
229229 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
230232 self . index += 1 ;
231233 // SAFETY: `i` is smaller than `self.len`, thus smaller than `self.a.len()` and `self.b.len()`
232234 unsafe {
233235 Some ( ( self . a . __iterator_get_unchecked ( i) , self . b . __iterator_get_unchecked ( i) ) )
234236 }
235237 } else if A :: MAY_HAVE_SIDE_EFFECT && self . index < self . a_len {
236238 let i = self . index ;
239+ // as above, increment before executing code that may panic
237240 self . index += 1 ;
238241 self . len += 1 ;
239242 // match the base implementation's potential side effects
@@ -259,6 +262,8 @@ where
259262 let end = self . index + delta;
260263 while self . index < end {
261264 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
262267 self . index += 1 ;
263268 if A :: MAY_HAVE_SIDE_EFFECT {
264269 // SAFETY: the usage of `cmp::min` to calculate `delta`
@@ -295,6 +300,8 @@ where
295300 let sz_a = self . a . size ( ) ;
296301 if A :: MAY_HAVE_SIDE_EFFECT && sz_a > self . len {
297302 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
298305 self . a_len -= 1 ;
299306 self . a . next_back ( ) ;
300307 }
@@ -309,6 +316,8 @@ where
309316 }
310317 }
311318 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
312321 self . len -= 1 ;
313322 self . a_len -= 1 ;
314323 let i = self . len ;
0 commit comments