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
#![no_std]

//! # unwrap_to
//! A simple utility macro that allows you to easily unwrap a ADT(Abstract
//! Data Type) enum into a reference of its inner type.
//! ```
//! #[macro_use] extern crate unwrap_to;
//!
//! enum Rule {
//!     String(String),
//!     Number(u64),
//! }
//!
//! fn main() {
//!     let rule = Rule::Number(7);
//!     assert_eq!(&7, unwrap_to!(rule => Rule::Number));
//! }
//! ```

//! A simple utility macro that allows you to easily unwrap a ADT(Abstract
//! Data Type) enum into a reference of its inner type.
/// ```
/// #[macro_use] extern crate unwrap_to;
///
/// enum Rule {
///     String(String),
///     Number(u64),
/// }
///
/// fn main() {
///     let rule = Rule::Number(7);
///     assert_eq!(&7, unwrap_to!(rule => Rule::Number));
/// }
/// ```
///
/// # Panics
///
/// The macro will panic as unreachable if the variant doesn't match.
///
#[macro_export]
macro_rules! unwrap_to {
    ($var:expr => $variant:path) => {
        match &$var {
            &$variant(ref v) => v,
            _ => unreachable!(),
        }
    }
}