Skip to content

Instantly share code, notes, and snippets.

@arifd
Last active March 14, 2023 18:17
Show Gist options
  • Save arifd/63772562d3d7e3758fe257fea69649d6 to your computer and use it in GitHub Desktop.
Save arifd/63772562d3d7e3758fe257fea69649d6 to your computer and use it in GitHub Desktop.
detect if token in macro present, branch inside macro
/// # Exmaple
/// ```
/// select! {
/// { $($yes)? } { if_present() } { if_absent() }
/// }
macro_rules! select {
({ $($_:tt)+ } { $($keep:tt)* } { $($discard:tt)* } ) => { $($keep)* };
({} { $($discard:tt)* } { $($keep:tt)* } ) => { $($keep)* };
}
//...
macro_rules! void {
($($_:tt)*) => {}
}
macro_rules optional_arg {
($($yes:tt)?) => {{
//... regular code
$({
void!($yes)
// some coniditonal code
})?
//... more regular code
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment