From 053f26de5a93248fa41592f203c244088237fe4c Mon Sep 17 00:00:00 2001 From: Sven Knobloch Date: Tue, 28 May 2019 13:33:50 +0200 Subject: [PATCH] Add implementation for DoubleEndedIterator for Zip with test --- src/ziptuple.rs | 24 ++++++++++++++++++++++++ tests/zip.rs | 16 ++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/ziptuple.rs b/src/ziptuple.rs index 2dc3ea5e0..c5054e0db 100644 --- a/src/ziptuple.rs +++ b/src/ziptuple.rs @@ -98,6 +98,30 @@ macro_rules! impl_zip_iter { $B: ExactSizeIterator, )* { } + + #[allow(non_snake_case)] + impl<$($B),*> DoubleEndedIterator for Zip<($($B,)*)> where + $( + $B: DoubleEndedIterator + ExactSizeIterator, + )* + { + #[inline] + fn next_back(&mut self) -> Option { + let ($(ref mut $B,)*) = self.t; + let size = *[$( $B.len(), )*].into_iter().min().unwrap(); + + $( + if $B.len() != size { + for _ in 0..$B.len() - size { $B.next_back(); } + } + )* + + match ($($B.next_back(),)*) { + ($(Some($B),)*) => Some(($($B,)*)), + _ => None, + } + } + } ); } diff --git a/tests/zip.rs b/tests/zip.rs index c5c51899b..2a4280369 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -3,6 +3,7 @@ extern crate itertools; use itertools::Itertools; use itertools::EitherOrBoth::{Both, Left, Right}; use itertools::free::zip_eq; +use itertools::multizip; #[test] fn zip_longest_fused() { @@ -42,6 +43,20 @@ fn test_double_ended_zip_longest() { assert_eq!(it.next(), None); } +#[test] +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 mut it = multizip((a, b)); + assert_eq!(it.next_back(), Some((4, 7))); + assert_eq!(it.next_back(), Some((3, 3))); + assert_eq!(it.next_back(), Some((2, 2))); + assert_eq!(it.next_back(), Some((1, 1))); + assert_eq!(it.next_back(), None); +} + #[should_panic] #[test] @@ -63,3 +78,4 @@ fn zip_eq_panic2() zip_eq(&a, &b).count(); } +