Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DoubleEndedIterator and FusedIterator for ZipEq. #531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/zip_eq_impl.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::iter::FusedIterator;

use super::size_hint;

/// An iterator which iterates two other iterators simultaneously
Expand Down Expand Up @@ -45,7 +47,7 @@ impl<I, J> Iterator for ZipEq<I, J>
(None, None) => None,
(Some(a), Some(b)) => Some((a, b)),
(None, Some(_)) | (Some(_), None) =>
panic!("itertools: .zip_eq() reached end of one iterator before the other")
panic!("itertools: .zip_eq() reached end of one iterator before the other")
}
}

Expand All @@ -54,7 +56,27 @@ impl<I, J> Iterator for ZipEq<I, J>
}
}

impl<I, J> DoubleEndedIterator for ZipEq<I, J>
where I: DoubleEndedIterator,
J: DoubleEndedIterator {
fn next_back(&mut self) -> Option<Self::Item> {
match (self.a.next_back(), self.b.next_back()) {
(None, None) => None,
(Some(a), Some(b)) => Some((a, b)),
(None, Some(_)) | (Some(_), None) =>
panic!("itertools: .zip_eq() reached start of one iterator before the other")
}
}
}

impl<I, J> ExactSizeIterator for ZipEq<I, J>
where I: ExactSizeIterator,
J: ExactSizeIterator
{}

// technically we only need at least one of the iterators to implement FusedIterator, but for that we either need
// negative trait bounds or maybe specialization
impl<I, J> FusedIterator for ZipEq<I, J>
where I: FusedIterator,
J: FusedIterator
{}
23 changes: 19 additions & 4 deletions tests/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ fn test_zip_longest_size_hint() {
fn test_double_ended_zip_longest() {
let xs = [1, 2, 3, 4, 5, 6];
let ys = [1, 2, 3, 7];
let a = xs.iter().map(|&x| x);
let b = ys.iter().map(|&x| x);
let a = xs.iter().copied();
let b = ys.iter().copied();
let mut it = a.zip_longest(b);
assert_eq!(it.next(), Some(Both(1, 1)));
assert_eq!(it.next(), Some(Both(2, 2)));
Expand All @@ -45,8 +45,8 @@ fn test_double_ended_zip_longest() {
fn test_double_ended_zip() {
let xs = [1, 2, 3, 4, 5, 6];
let ys = [1, 2, 3, 7];
let a = xs.iter().map(|&x| x);
let b = ys.iter().map(|&x| x);
let a = xs.iter().copied();
let b = ys.iter().copied();
let mut it = multizip((a, b));
assert_eq!(it.next_back(), Some((4, 7)));
assert_eq!(it.next_back(), Some((3, 3)));
Expand Down Expand Up @@ -75,3 +75,18 @@ fn zip_eq_panic2()

zip_eq(&a, &b).count();
}

#[test]
fn zip_eq_backwards() {
let mut it = zip_eq(0..3, 4..7);
assert_eq!(it.next_back(), Some((2, 6)));
assert_eq!(it.next_back(), Some((1, 5)));
assert_eq!(it.next_back(), Some((0, 4)));
assert_eq!(it.next_back(), None);
}

#[should_panic]
#[test]
fn zip_eq_backwards_panic() {
zip_eq(&[1, 2], &[1, 2, 3]).rev().count();
}