Skip to content

Commit 8b87335

Browse files
author
sarah
committed
apply review suggestions
1 parent 1b6e730 commit 8b87335

File tree

1 file changed

+10
-26
lines changed
  • library/core/src/iter/adapters

1 file changed

+10
-26
lines changed

library/core/src/iter/adapters/zip.rs

+10-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::cmp;
22
use crate::fmt::{self, Debug};
33
use crate::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator, Iterator};
44
use crate::iter::{InPlaceIterable, SourceIter, TrustedLen};
5-
use crate::ops::{ControlFlow, Try};
5+
use crate::ops::{ControlFlow, NeverShortCircuit, Try};
66

77
/// An iterator that iterates two other iterators simultaneously.
88
///
@@ -43,13 +43,9 @@ impl<A: Iterator, B: Iterator> Zip<A, B> {
4343
if a_sz != b_sz {
4444
// Adjust a, b to equal length
4545
if a_sz > b_sz {
46-
for _ in 0..a_sz - b_sz {
47-
self.a.next_back();
48-
}
46+
let _ = self.a.advance_back_by(a_sz - b_sz);
4947
} else {
50-
for _ in 0..b_sz - a_sz {
51-
self.b.next_back();
52-
}
48+
let _ = self.b.advance_back_by(b_sz - a_sz);
5349
}
5450
}
5551
}
@@ -210,11 +206,6 @@ trait ZipImpl<A, B> {
210206
Self: Iterator + TrustedRandomAccessNoCoerce;
211207
}
212208

213-
#[inline]
214-
fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
215-
move |acc, x| Ok(f(acc, x))
216-
}
217-
218209
#[inline]
219210
fn check_rfold<AItem, B: DoubleEndedIterator, T, F: FnMut(T, (AItem, B::Item)) -> T>(
220211
mut b: B,
@@ -227,16 +218,9 @@ fn check_rfold<AItem, B: DoubleEndedIterator, T, F: FnMut(T, (AItem, B::Item)) -
227218
}
228219

229220
#[inline]
230-
fn check_try_fold<
231-
'b,
232-
AItem,
233-
B: Iterator,
234-
T,
235-
R: Try<Output = T>,
236-
F: 'b + FnMut(T, (AItem, B::Item)) -> R,
237-
>(
238-
b: &'b mut B,
239-
mut f: F,
221+
fn check_try_fold<'b, AItem, BItem, T, R: Try<Output = T>>(
222+
b: &'b mut impl Iterator<Item = BItem>,
223+
mut f: impl 'b + FnMut(T, (AItem, BItem)) -> R,
240224
) -> impl 'b + FnMut(T, AItem) -> ControlFlow<R, T> {
241225
move |acc, x| match b.next() {
242226
Some(y) => ControlFlow::from_try(f(acc, (x, y))),
@@ -311,7 +295,7 @@ macro_rules! zip_impl_general_defaults {
311295
where
312296
F: FnMut(T, Self::Item) -> T,
313297
{
314-
ZipImpl::try_fold(&mut self, init, ok(f)).unwrap()
298+
ZipImpl::try_fold(&mut self, init, NeverShortCircuit::wrap_mut_2(f)).0
315299
}
316300

317301
#[inline]
@@ -407,6 +391,9 @@ where
407391
}
408392
}
409393

394+
/// Adjusts a, b to equal length. Makes sure that only the first call
395+
/// of `next_back` does this, otherwise we will break the restriction
396+
/// on calls to `zipped.next_back()` after calling `get_unchecked()`.
410397
#[inline]
411398
fn adjust_back_trusted_random_access<
412399
A: TrustedRandomAccess + DoubleEndedIterator + ExactSizeIterator,
@@ -417,9 +404,6 @@ fn adjust_back_trusted_random_access<
417404
if A::MAY_HAVE_SIDE_EFFECT || B::MAY_HAVE_SIDE_EFFECT {
418405
let sz_a = zipped.a.size();
419406
let sz_b = zipped.b.size();
420-
// Adjust a, b to equal length, make sure that only the first call
421-
// of `next_back` does this, otherwise we will break the restriction
422-
// on calls to `zipped.next_back()` after calling `get_unchecked()`.
423407
if sz_a != sz_b {
424408
let sz_a = zipped.a.size();
425409
if A::MAY_HAVE_SIDE_EFFECT && sz_a > zipped.len {

0 commit comments

Comments
 (0)