Created
August 2, 2019 23:03
-
-
Save rust-play/8032a5465e391fedf518528ccee6994b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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