Skip to content

Commit

Permalink
Merge #507
Browse files Browse the repository at this point in the history
507: Make macros more hygienic and callable without importing them r=jswrenn a=SkiFire13

Fixes #506

Co-authored-by: Giacomo Stevanato <giaco.stevanato@gmail.com>
  • Loading branch information
bors[bot] and SkiFire13 committed Dec 18, 2020
2 parents 00756e0 + 79e39cb commit 5ad3e1a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/lib.rs
Expand Up @@ -253,16 +253,16 @@ macro_rules! iproduct {
$I
);
(@flatten $I:expr, $J:expr, $($K:expr,)*) => (
iproduct!(@flatten $crate::cons_tuples(iproduct!($I, $J)), $($K,)*)
$crate::iproduct!(@flatten $crate::cons_tuples($crate::iproduct!($I, $J)), $($K,)*)
);
($I:expr) => (
$crate::__std_iter::IntoIterator::into_iter($I)
);
($I:expr, $J:expr) => (
$crate::Itertools::cartesian_product(iproduct!($I), iproduct!($J))
$crate::Itertools::cartesian_product($crate::iproduct!($I), $crate::iproduct!($J))
);
($I:expr, $J:expr, $($K:expr),+) => (
iproduct!(@flatten iproduct!($I, $J), $($K,)+)
$crate::iproduct!(@flatten $crate::iproduct!($I, $J), $($K,)+)
);
}

Expand Down Expand Up @@ -313,7 +313,7 @@ macro_rules! izip {

// The "b" identifier is a different identifier on each recursion level thanks to hygiene.
( @closure $p:pat => ( $($tup:tt)* ) , $_iter:expr $( , $tail:expr )* ) => {
izip!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
$crate::izip!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
};

// unary
Expand All @@ -323,18 +323,18 @@ macro_rules! izip {

// binary
($first:expr, $second:expr $(,)*) => {
izip!($first)
$crate::izip!($first)
.zip($second)
};

// n-ary where n > 2
( $first:expr $( , $rest:expr )* $(,)* ) => {
izip!($first)
$crate::izip!($first)
$(
.zip($rest)
)*
.map(
izip!(@closure a => (a) $( , $rest )*)
$crate::izip!(@closure a => (a) $( , $rest )*)
)
};
}
Expand Down
13 changes: 13 additions & 0 deletions tests/macros_hygiene.rs
@@ -0,0 +1,13 @@
#[test]
fn iproduct_hygiene() {
let _ = itertools::iproduct!(0..6);
let _ = itertools::iproduct!(0..6, 0..9);
let _ = itertools::iproduct!(0..6, 0..9, 0..12);
}

#[test]
fn izip_hygiene() {
let _ = itertools::izip!(0..6);
let _ = itertools::izip!(0..6, 0..9);
let _ = itertools::izip!(0..6, 0..9, 0..12);
}

0 comments on commit 5ad3e1a

Please sign in to comment.