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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Macros for conditional compilation.

/// Compile-time matching on config variables.
///
/// Only the branch relevant on your machine will be type-checked!
///
/// # Example
///
/// ```
/// # #[macro_use] extern crate mac;
/// # fn main() {
/// let mascot = match_cfg! {
///     (target_os = "linux") => "penguin",
///     (target_os = "openbsd") => "blowfish",
///     _ => "unknown",
/// };
/// println!("{}", mascot);
/// # }
/// ```
///
#[macro_export]
macro_rules! match_cfg {
    ( $( ($cfg:meta) => $e:expr, )* _ => $last:expr, ) => {
        match () {
            $(
                #[cfg($cfg)]
                () => $e,
            )*

            #[cfg(all( $( not($cfg) ),* ))]
            () => $last,
        }
    };

    ( $( ($cfg:meta) => $e:expr, )* ) => {
        match_cfg! {
            $(
                ($cfg) => $e,
            )*

            _ => {
                #[allow(dead_code)]
                #[static_assert]
                static MATCH_CFG_FALLBACK_UNREACHABLE: bool = false;
            }
        }
    };
}

/// Compile-time conditional expression.
///
/// # Example
///
/// ```
/// # #[macro_use] extern crate mac;
/// # fn main() {
/// if_cfg!(test {
///     println!("Crate built as a test suite");
/// })
/// # }
/// ```
///
/// Unlike `if cfg!(...)`, this will not even compile the unused branch.
///
/// ```
/// # #[macro_use] extern crate mac;
/// # fn main() {
/// let x = if_cfg!(any(bleh, blah="bluh") {
///     some_undefined_function_name();
///     2 + "doesn't even typecheck"
/// } else {
///     3
/// });
///
/// assert_eq!(x, 3);
/// # }
/// ```
#[macro_export]
macro_rules! if_cfg {
    ($cfg:meta $t:block else $f:block) => {
        match_cfg! {
            ($cfg) => $t,
            _ => $f,
        }
    };

    ($cfg:meta $t:block) => {
        if_cfg!($cfg $t else { })
    };
}