Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 2, 2019 23:03
Show Gist options
  • Save rust-play/8032a5465e391fedf518528ccee6994b to your computer and use it in GitHub Desktop.
Save rust-play/8032a5465e391fedf518528ccee6994b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
macro_rules! or_else {
(,$b:tt) => {
stringify!($b)
};
($a:tt, $b:tt) => {
stringify!($a)
};
}
macro_rules! func {
($export:ident $(: $native:ident)?($($args:tt),*)$(-> $ret:ident)?, $($u:tt)*) => {
func!($export $(: $native)?($($args),*)$(-> $ret)?);
func!($($u)*);
};
($export:ident $(: $native:ident)?($($args:tt),*)$(-> $ret:ident)?) => {
println!(
r#" "{}": extern "C" fn {}({}) -> {}"#,
stringify!($export),
or_else!($($native)?, $export),
stringify!($($args),*),
or_else!($($ret)?, ())
);
};
}
macro_rules! ns {
($ns:ident: {
$($t:tt)*
},$($u:tt)*) => {
ns!($ns: {
$($t)*
});
ns!($($u)*);
};
($ns:ident: {
$($t:tt)*
}) => {
println!(r#""{}": {{"#, stringify!($ns));
func!($($t)*);
println!("}}");
};
}
fn main() {
ns!( wasi: {
fd_read(qsdf,qsdf),
fd_write: mbed_os_fd_write(txt, len),
bye(qa, ppl)
});
ns!(mbed_os: {
gpio_set: mbed_os_gpio_set(u32, bool),
gpio_get: mbed_os_gpio_get(u32) -> bool
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment