Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Created September 9, 2022 12:48
Show Gist options
  • Select an option

  • Save MikuroXina/0907d4c42f657af2a566e14778630817 to your computer and use it in GitHub Desktop.

Select an option

Save MikuroXina/0907d4c42f657af2a566e14778630817 to your computer and use it in GitHub Desktop.
Generate permutations on macro arguments in Rust.
macro_rules! pairs {
($macro:ident; $($items:literal),*) => {
pairs!(@ $macro; $($items),*; $($items),*);
};
(@ $macro:ident; $x:literal; $($ys:literal),*) => {
$(
$macro!($x, $ys);
)*
};
(@ $macro:ident; $x:literal, $($xs:literal),*; $($ys:literal),*) => {
$(
$macro!($x, $ys);
)*
pairs!(@ $macro; $($xs),*; $($ys),*);
};
}
macro_rules! triplets {
($macro:ident; $($items:literal),*) => {
triplets!(@ $macro; $($items),*; $($items),*; $($items),*);
};
(@ $macro:ident; $x:literal; $y:literal; $($zs:literal),*) => {
$(
$macro!($x, $y, $zs);
)*
};
(@ $macro:ident; $x:literal; $y:literal, $($ys:literal),*; $($zs:literal),*) => {
$(
$macro!($x, $y, $zs);
)*
triplets!(@ $macro; $x; $($ys),*; $($zs),*);
};
(@ $macro:ident; $x:literal, $($xs:literal),*; $y:literal, $($ys:literal),*; $($zs:literal),*) => {
$(
$macro!($x, $y, $zs);
)*
triplets!(@ $macro; $($xs),*; $y, $($ys),*; $($zs),*);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment