Skip to content

Commit

Permalink
Ensure panic on length mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kinto-b committed Apr 15, 2024
1 parent a373e69 commit 18e352d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/zip_eq_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,23 @@ where
}
}

#[inline]
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
let Self { a, mut b } = self;

a.fold(init, |acc, x| match b.next() {
let acc = a.fold(init, |acc, x| match b.next() {
Some(y) => f(acc, (x, y)),
None => panic!("itertools: .zip_eq() reached end of one iterator before the other"),
})
});

if b.next().is_some() {
panic!("itertools: .zip_eq() reached end of one iterator before the other")
}

acc
}

fn size_hint(&self) -> (usize, Option<usize>) {
Expand Down
9 changes: 9 additions & 0 deletions tests/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,12 @@ fn zip_eq_panic3() {

zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y);
}

#[should_panic]
#[test]
fn zip_eq_panic4() {
let b = [1, 2];
let a = [1, 2, 3];

zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y);
}

0 comments on commit 18e352d

Please sign in to comment.