Last active
March 14, 2023 18:17
-
-
Save arifd/63772562d3d7e3758fe257fea69649d6 to your computer and use it in GitHub Desktop.
detect if token in macro present, branch inside macro
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
/// # 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