Skip to content

Instantly share code, notes, and snippets.

@djg djg/macros.rs
Created May 27, 2019

Embed
What would you like to do?
Simulating named parameters with rust macros
#[macro_export]
macro_rules! signal_build {
($name:expr, $shape:expr, $reset:expr, $reset_less:expr) => {
Signal::signal($name, Shape::from($shape), $reset, $reset_less)
};
}
#[macro_export(local_inner_macros)]
macro_rules! signal_collect {
(($_:expr, $shape:expr, $reset:expr, $reset_less:expr) name: $name:expr, $($k:ident: $v:expr,)*) => {
signal_collect!(($name, $shape, $reset, $reset_less) $($k: $v,)*)
};
(($name:expr, $_:expr, $reset:expr, $reset_less:expr) shape: $shape:expr, $($k:ident: $v:expr,)*) => {
signal_collect!(($name, $shape, $reset, $reset_less) $($k: $v,)*)
};
(($name:expr, $shape:expr, $_:expr, $reset_less:expr) reset: $reset:expr, $($k:ident: $v:expr,)*) => {
signal_collect!(($name, $shape, $reset, $reset_less) $($k: $v,)*)
};
(($name:expr, $shape:expr, $reset:expr, $_:expr) reset_less: $reset_less:expr, $($k:ident: $v:expr,)*) => {
signal_collect!(($name, $shape, $reset, $reset_less) $($k: $v,)*)
};
(($name:expr, $shape:expr, $reset:expr, $reset_less:expr)) => {
signal_build!($name, $shape, $reset, $reset_less)
};
}
// use like:
// signal!(0);
// signal!(8, true);
// signal!(name: "s1", shape: -10..10);
// signal!(name: "s1", shape: 8, reset: 0xFF, reset_less: true);
#[macro_export(local_inner_macros)]
macro_rules! signal {
($($k:ident: $v:expr,)*) => {
signal_collect!(("$signal", Shape::default(), 0, false) $($k: $v,)*)
};
($($k:ident: $v:expr),*) => {
signal!($($k: $v,)*)
};
($bits:expr) => {
signal!(shape: $bits,)
};
($bits:expr, $($k:ident: $v:expr),*) => {
signal!(shape: $bits, $($k: $v,)*)
};
($bits:expr, $signed:expr) => {
signal!(shape: Shape { bits: $bits, signed: $signed },)
};
($bits:expr, $signed:expr, $($k:ident: $v:expr),*) => {
signal!(shape: Shape { bits: $bits, signed: $signed }, $($k: $v,)*)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.