Skip to content

Commit c0dd154

Browse files
committed
Simplify multizip reduction macros
1 parent 963a4d3 commit c0dd154

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

src/iter/multizip.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use super::plumbing::*;
22
use super::*;
33

4-
use std::cmp;
5-
64
/// `MultiZip` is an iterator that zips up a tuple of parallel iterators to
75
/// produce tuples of their items.
86
///
@@ -92,44 +90,36 @@ pub struct MultiZip<T> {
9290
//
9391
// But if we ever increase to 13, we would want to split 8,5 rather than 4,9.
9492

95-
macro_rules! zip {
96-
($a:expr, $b:expr, $c:expr, $d:expr, $( $x:expr, )+) => {
97-
zip!(zip!($a, $b, $c, $d,), zip!($( $x, )+),)
98-
};
99-
($a:expr, $b:expr, $( $x:expr, )+) => {
100-
zip!(zip!($a, $b,), zip!($( $x, )+),)
101-
};
102-
($a:expr, $( $x:expr, )*) => {
103-
$a $( .zip($x) )*
104-
};
105-
}
106-
107-
macro_rules! min {
108-
($a:expr, $b:expr, $c:expr, $d:expr, $( $x:expr, )+) => {
109-
min!(min!($a, $b, $c, $d,), min!($( $x, )+),)
93+
macro_rules! reduce {
94+
($a:expr, $b:expr, $c:expr, $d:expr, $( $x:expr ),+ => $fn:path) => {
95+
reduce!(reduce!($a, $b, $c, $d => $fn),
96+
reduce!($( $x ),+ => $fn)
97+
=> $fn)
11098
};
111-
($a:expr, $b:expr, $( $x:expr, )+) => {
112-
min!(min!($a, $b,), min!($( $x, )+),)
99+
($a:expr, $b:expr, $( $x:expr ),+ => $fn:path) => {
100+
reduce!(reduce!($a, $b => $fn),
101+
reduce!($( $x ),+ => $fn)
102+
=> $fn)
113103
};
114-
($a:expr, $b:expr,) => { cmp::min($a, $b) };
115-
($a:expr,) => { $a };
104+
($a:expr, $b:expr => $fn:path) => { $fn($a, $b) };
105+
($a:expr => $fn:path) => { $a };
116106
}
117107

118108
macro_rules! nest {
119-
($A:tt, $B:tt, $C:tt, $D:tt, $( $X:tt, )+) => {
120-
(nest!($A, $B, $C, $D,), nest!($( $X, )+))
109+
($A:tt, $B:tt, $C:tt, $D:tt, $( $X:tt ),+) => {
110+
(nest!($A, $B, $C, $D), nest!($( $X ),+))
121111
};
122-
($A:tt, $B:tt, $( $X:tt, )+) => {
123-
(($A, $B), nest!($( $X, )+))
112+
($A:tt, $B:tt, $( $X:tt ),+) => {
113+
(($A, $B), nest!($( $X ),+))
124114
};
125-
($A:tt, $B:tt,) => { ($A, $B) };
126-
($A:tt,) => { $A };
115+
($A:tt, $B:tt) => { ($A, $B) };
116+
($A:tt) => { $A };
127117
}
128118

129119
macro_rules! flatten {
130-
($( $T:ident, )+) => {{
120+
($( $T:ident ),+) => {{
131121
#[allow(non_snake_case)]
132-
fn flatten<$( $T ),+>(nest!($( $T, )+) : nest!($( $T, )+)) -> ($( $T, )+) {
122+
fn flatten<$( $T ),+>(nest!($( $T ),+) : nest!($( $T ),+)) -> ($( $T, )+) {
133123
($( $T, )+)
134124
}
135125
flatten
@@ -220,21 +210,21 @@ macro_rules! multizip_impls {
220210
where
221211
CONSUMER: Consumer<Self::Item>,
222212
{
223-
zip!($( self.tuple.$idx, )+)
224-
.map(flatten!($( $T, )+))
213+
reduce!($( self.tuple.$idx ),+ => IndexedParallelIterator::zip)
214+
.map(flatten!($( $T ),+))
225215
.drive(consumer)
226216
}
227217

228218
fn len(&self) -> usize {
229-
min!($( self.tuple.$idx.len(), )+)
219+
reduce!($( self.tuple.$idx.len() ),+ => Ord::min)
230220
}
231221

232222
fn with_producer<CB>(self, callback: CB) -> CB::Output
233223
where
234224
CB: ProducerCallback<Self::Item>,
235225
{
236-
zip!($( self.tuple.$idx, )+)
237-
.map(flatten!($( $T, )+))
226+
reduce!($( self.tuple.$idx ),+ => IndexedParallelIterator::zip)
227+
.map(flatten!($( $T ),+))
238228
.with_producer(callback)
239229
}
240230
}

0 commit comments

Comments
 (0)