@@ -13,9 +13,10 @@ use crate::iter::{InPlaceIterable, SourceIter, TrustedLen};
13
13
pub struct Zip < A , B > {
14
14
a : A ,
15
15
b : B ,
16
- // index and len are only used by the specialized version of zip
16
+ // index, len and a_len are only used by the specialized version of zip
17
17
index : usize ,
18
18
len : usize ,
19
+ a_len : usize ,
19
20
}
20
21
impl < A : Iterator , B : Iterator > Zip < A , B > {
21
22
pub ( in crate :: iter) fn new ( a : A , b : B ) -> Zip < A , B > {
@@ -110,6 +111,7 @@ where
110
111
b,
111
112
index : 0 , // unused
112
113
len : 0 , // unused
114
+ a_len : 0 , // unused
113
115
}
114
116
}
115
117
@@ -184,8 +186,9 @@ where
184
186
B : TrustedRandomAccess + Iterator ,
185
187
{
186
188
fn new ( a : A , b : B ) -> Self {
187
- let len = cmp:: min ( a. size ( ) , b. size ( ) ) ;
188
- Zip { a, b, index : 0 , len }
189
+ let a_len = a. size ( ) ;
190
+ let len = cmp:: min ( a_len, b. size ( ) ) ;
191
+ Zip { a, b, index : 0 , len, a_len }
189
192
}
190
193
191
194
#[ inline]
@@ -197,7 +200,7 @@ where
197
200
unsafe {
198
201
Some ( ( self . a . __iterator_get_unchecked ( i) , self . b . __iterator_get_unchecked ( i) ) )
199
202
}
200
- } else if A :: MAY_HAVE_SIDE_EFFECT && self . index < self . a . size ( ) {
203
+ } else if A :: MAY_HAVE_SIDE_EFFECT && self . index < self . a_len {
201
204
let i = self . index ;
202
205
self . index += 1 ;
203
206
self . len += 1 ;
@@ -262,6 +265,7 @@ where
262
265
for _ in 0 ..sz_a - self . len {
263
266
self . a . next_back ( ) ;
264
267
}
268
+ self . a_len = self . len ;
265
269
}
266
270
let sz_b = self . b . size ( ) ;
267
271
if B :: MAY_HAVE_SIDE_EFFECT && sz_b > self . len {
@@ -273,6 +277,7 @@ where
273
277
}
274
278
if self . index < self . len {
275
279
self . len -= 1 ;
280
+ self . a_len -= 1 ;
276
281
let i = self . len ;
277
282
// SAFETY: `i` is smaller than the previous value of `self.len`,
278
283
// which is also smaller than or equal to `self.a.len()` and `self.b.len()`
0 commit comments