Skip to content

Commit 594c27a

Browse files
Sven Knoblochjswrenn
Sven Knobloch
authored andcommitted
Add implementation for DoubleEndedIterator for Zip with test
1 parent c48da7b commit 594c27a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/ziptuple.rs

+24
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ macro_rules! impl_zip_iter {
9898
$B: ExactSizeIterator,
9999
)*
100100
{ }
101+
102+
#[allow(non_snake_case)]
103+
impl<$($B),*> DoubleEndedIterator for Zip<($($B,)*)> where
104+
$(
105+
$B: DoubleEndedIterator + ExactSizeIterator,
106+
)*
107+
{
108+
#[inline]
109+
fn next_back(&mut self) -> Option<Self::Item> {
110+
let ($(ref mut $B,)*) = self.t;
111+
let size = *[$( $B.len(), )*].into_iter().min().unwrap();
112+
113+
$(
114+
if $B.len() != size {
115+
for _ in 0..$B.len() - size { $B.next_back(); }
116+
}
117+
)*
118+
119+
match ($($B.next_back(),)*) {
120+
($(Some($B),)*) => Some(($($B,)*)),
121+
_ => None,
122+
}
123+
}
124+
}
101125
);
102126
}
103127

tests/zip.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use itertools::Itertools;
22
use itertools::EitherOrBoth::{Both, Left, Right};
33
use itertools::free::zip_eq;
4+
use itertools::multizip;
45

56
#[test]
67
fn zip_longest_fused() {
@@ -40,6 +41,20 @@ fn test_double_ended_zip_longest() {
4041
assert_eq!(it.next(), None);
4142
}
4243

44+
#[test]
45+
fn test_double_ended_zip() {
46+
let xs = [1, 2, 3, 4, 5, 6];
47+
let ys = [1, 2, 3, 7];
48+
let a = xs.iter().map(|&x| x);
49+
let b = ys.iter().map(|&x| x);
50+
let mut it = multizip((a, b));
51+
assert_eq!(it.next_back(), Some((4, 7)));
52+
assert_eq!(it.next_back(), Some((3, 3)));
53+
assert_eq!(it.next_back(), Some((2, 2)));
54+
assert_eq!(it.next_back(), Some((1, 1)));
55+
assert_eq!(it.next_back(), None);
56+
}
57+
4358

4459
#[should_panic]
4560
#[test]
@@ -61,3 +76,4 @@ fn zip_eq_panic2()
6176
zip_eq(&a, &b).count();
6277
}
6378

79+

0 commit comments

Comments
 (0)