1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! This is a minimal crate which implements match through different types.
//!
//! Usage:
//!
//! ```rust
//! #[macro_use]
//! extern crate match_cast;
//! use std::panic;
//!
//! fn main() {
//!     let res = panic::catch_unwind(|| {
//!         panic!("Oh no!");
//!     });
//!
//!     let any = res.unwrap_err();
//!
//!     let result = match_cast!( any {
//!         val as Option<u8> => {
//!             format!("Option<u8> = {:?}", val)
//!         },
//!         val as String => {
//!             format!("String = {:?}", val)
//!         },
//!         val as &'static str => {
//!             format!("&'static str = {:?}", val)
//!         },
//!     });
//!
//!     assert_eq!(result.unwrap(), "&'static str = \"Oh no!\"");
//! }
//! ```
//!

#[macro_export]
macro_rules! match_cast {
    ($any:ident { $( $bind:ident as $patt:ty => $body:block , )+ }) => {{
        let downcast = || {
            $(
            if let Some($bind) = $any.downcast_ref::<$patt>() {
                return Some($body);
            }
            )+
            None
        };
        downcast()
    }};
}