Skip to content

Commit 3ca656c

Browse files
authored
Merge pull request #256 from bluss/binary-zip
Let the resulting type of izip!(i, j) be std::iter::Zip<i, j>
2 parents 182c0cf + 567c48e commit 3ca656c

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/lib.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,15 @@ macro_rules! iproduct {
214214
/// returns `None`.
215215
///
216216
/// This is a version of the standard ``.zip()`` that's supporting more than
217-
/// two iterators. The iterator elment type is a tuple with one element
217+
/// two iterators. The iterator element type is a tuple with one element
218218
/// from each of the input iterators. Just like ``.zip()``, the iteration stops
219219
/// when the shortest of the inputs reaches its end.
220220
///
221-
/// **Note:** The result of this macro is an iterator composed of
222-
/// repeated `.zip()` and a `.map()`; it has an anonymous type.
221+
/// **Note:** The result of this macro is in the general case an iterator
222+
/// composed of repeated `.zip()` and a `.map()`; it has an anonymous type.
223+
/// The special cases of one and two arguments produce the equivalent of
224+
/// `$a.into_iter()` and `$a.into_iter().zip($b)` respectively.
225+
///
223226
/// Prefer this macro `izip!()` over [`multizip`] for the performance benefits
224227
/// of using the standard library `.zip()`.
225228
///
@@ -261,8 +264,20 @@ macro_rules! izip {
261264
izip!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
262265
};
263266

264-
( $first:expr $( , $rest:expr )* $(,)* ) => {
267+
// unary
268+
($first:expr $(,)*) => {
265269
$crate::__std_iter::IntoIterator::into_iter($first)
270+
};
271+
272+
// binary
273+
($first:expr, $second:expr $(,)*) => {
274+
izip!($first)
275+
.zip($second)
276+
};
277+
278+
// n-ary where n > 2
279+
( $first:expr $( , $rest:expr )* $(,)* ) => {
280+
izip!($first)
266281
$(
267282
.zip($rest)
268283
)*

tests/test_core.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#[macro_use] extern crate itertools as it;
99

10+
use core::iter;
11+
1012
use it::flatten;
1113
use it::Itertools;
1214
use it::interleave;
@@ -54,8 +56,23 @@ fn izip_macro() {
5456
assert!(zip.next().is_none());
5557
}
5658

59+
#[test]
60+
fn izip2() {
61+
let _zip1: iter::Zip<_, _> = izip!(1.., 2..);
62+
let _zip2: iter::Zip<_, _> = izip!(1.., 2.., );
63+
}
64+
5765
#[test]
5866
fn izip3() {
67+
let mut zip: iter::Map<iter::Zip<_, _>, _> = izip!(0..3, 0..2, 0..2i8);
68+
for i in 0..2 {
69+
assert!((i as usize, i, i as i8) == zip.next().unwrap());
70+
}
71+
assert!(zip.next().is_none());
72+
}
73+
74+
#[test]
75+
fn multizip3() {
5976
let mut zip = multizip((0..3, 0..2, 0..2i8));
6077
for i in 0..2 {
6178
assert!((i as usize, i, i as i8) == zip.next().unwrap());

0 commit comments

Comments
 (0)