Skip to content

Instantly share code, notes, and snippets.

@durka
Created September 25, 2017 21:44
Show Gist options
  • Save durka/5846f4b2ec7c09446562822d08af6b42 to your computer and use it in GitHub Desktop.
Save durka/5846f4b2ec7c09446562822d08af6b42 to your computer and use it in GitHub Desktop.
/// Try downcasting an Any to several types, performing the same or different actions if one of the
/// types matches, otherwise do a fallback
///
/// Syntax is similar to `match`, except the types are separated by commas and the _ case comes
/// first (TODO fix this)
#[macro_export]
macro_rules! downcast {
($any:ident { _ => $code:expr, $($($ts:ty),+ => $codes:expr),* }) => {
$(
$(
if let Some($any) = $any.downcast_ref::<$ts>() {
$codes
}
)else+
)else*
else { $code }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment