Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created June 10, 2015 05:50
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alexcrichton/29b618d75cde5b57d797 to your computer and use it in GitHub Desktop.
Save alexcrichton/29b618d75cde5b57d797 to your computer and use it in GitHub Desktop.
macro_rules! cascade {
($(
if #[cfg($($meta:meta),*)] { $($it:item)* }
) else * else {
$($it2:item)*
}) => {
__items! {
() ;
$( ( ($($meta),*) ($($it)*) ), )*
( () ($($it2)*) ),
}
}
}
macro_rules! __items {
(($($not:meta,)*) ; ) => {};
(($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
__apply! { cfg(all($($m,)* not(any($($not),*)))), $($it)* }
__items! { ($($not,)* $($m,)*) ; $($rest)* }
}
}
macro_rules! __apply {
($m:meta, $($it:item)*) => {
$(#[$m] $it)*
}
}
cascade! {
if #[cfg(feature = "libunwind")] {
mod libunwind;
pub use self::libunwind::trace;
} else if #[cfg(feature = "unix-backtrace")] {
mod unix_backtrace;
pub use self::unix_backtrace::trace;
} else {
mod noop;
pub use self::noop::trace;
}
}
@shanemikel
Copy link

Nice! I'm not gonna comment on that Lispy voodoo, but it seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment