Skip to content

Instantly share code, notes, and snippets.

@durka
Last active September 4, 2018 05:24
Show Gist options
  • Save durka/ea4827c86fce413fff887a9d70859074 to your computer and use it in GitHub Desktop.
Save durka/ea4827c86fce413fff887a9d70859074 to your computer and use it in GitHub Desktop.
fn main() -> ! {
let (mut delay, mut leds): (Delay, Leds) = aux5::init();
loop {
foreach!($
state => [on off]
period => [500 100]
{
for led in 0..8 {
leds[led].$state();
}
delay.delay_ms($period as u16);
});
}
}
macro_rules! foreach {
(@go
() ->
([$($name:ident)*] [$([$($variant:tt)*])*])
[$d:tt { $($body:tt)* }]
) => {{
macro_rules! __foreach_substitute {
($($d$name:tt)*) => {
$($body)*
}
}
$(__foreach_substitute!($($variant)*);)*
}};
(@go
($name:ident => [$($variant:tt)*] $($rest:tt)*) ->
([$($names:tt)*] [$([$($variants:tt)*])*])
$thru:tt
) => {
foreach!(@go
($($rest)*) ->
([$($names)* $name] [$([$($variants)* $variant])*])
$thru);
};
(
$d:tt
$name:ident => [$($variant:tt)*]
$($names:ident => [$($variants:tt)*])*
{ $($body:tt)* }
) => {
foreach!(@go
($($names => [$($variants)*])*) ->
([$name] [$([$variant])*])
[$d { $($body)* }]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment