root_io/utils.rs
1/// Zip `n` streams together analogous to the `zip()` function. This
2/// is useful when iterating over multiple branches of a TTree. See
3/// the examples of this crate for the suggested use-case.
4#[macro_export]
5macro_rules! stream_zip {
6 // @closure creates a tuple-flattening closure for .map() call. usage:
7 // @closure partial_pattern => partial_tuple , rest , of , iterators
8 // eg. izip!( @closure ((a, b), c) => (a, b, c) , dd , ee )
9 ( @closure $p:pat => $tup:expr ) => {
10 |$p| $tup
11 };
12
13 // The "b" identifier is a different identifier on each recursion level thanks to hygiene.
14 ( @closure $p:pat => ( $($tup:tt)* ) , $_iter:expr $( , $tail:expr )* ) => {
15 stream_zip!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
16 };
17
18 // unary
19 ($first:expr $(,)*) => {
20 $first
21 };
22
23 // binary
24 ($first:expr, $second:expr $(,)*) => {
25 stream_zip!($first)
26 .zip($second)
27 };
28
29 // n-ary where n > 2
30 ( $first:expr $( , $rest:expr )* $(,)* ) => {
31 stream_zip!($first)
32 $(
33 .zip($rest)
34 )*
35 .map(
36 stream_zip!(@closure a => (a) $( , $rest )*)
37 )
38 };
39}