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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! A convenience macro to ergonomically define an item depending on a large
//! number of `#[cfg]` parameters. Structured like match statement, the first
//! matching branch is the item that gets emitted.

#![cfg_attr(not(feature = "use_core"), feature(no_core))]
#![doc(html_root_url = "https://docs.rs/cfg-if")]
#![cfg_attr(test, deny(warnings))]
#![cfg_attr(not(feature = "use_core"), no_core)]
#![cfg_attr(feature = "use_core", no_std)]

/// The macro provided by this crate, `match_cfg`, is similar to the `if/elif` C
/// preprocessor directives and allows defining a cascade of `#[cfg]` cases,
/// emitting the implementation which matches first.
///
/// This conveniently allows providing a long list `#[cfg]`'d blocks of code
/// without having to rewrite each `cfg()` clause multiple times.
///
/// # Example
///
/// ```
/// #[macro_use(match_cfg)]
/// extern crate match_cfg;
///
/// match_cfg! {
///     #[cfg(unix)] => {
///         fn foo() { /* unix specific functionality */ }
///     }
///     #[cfg(target_pointer_width = "32")] => {
///         fn foo() { /* non-unix, 32-bit functionality */ }
///     }
///     _ => {
///         fn foo() { /* fallback implementation */ }
///     }
/// }
/// # fn main() {}
/// ```
#[macro_export]
macro_rules! match_cfg {
    (#[cfg($cfg:meta)] => { $($i:item)* }) => {
        $(
            #[cfg($cfg)] $i
        )*
    };
    (#[cfg($cfg:meta)] @ #[cfg($cfg_not:meta)] => { $($i:item)* }) => {
        $(
            #[cfg(not($cfg_not))] #[cfg($cfg)] $i
        )*
    };
    (_ => { $($i:item)* }) => { $( $i )* };
    (_ @ #[cfg($cfg_not:meta)] => { $($i:item)* }) => {
        $(
            #[cfg(not($cfg_not))] $i
        )*
    };
    (
        #[cfg($cfg0:meta)] => { $($i:item)* }
        $(#[cfg($cfgs:meta)] => { $($is:item)* })*
    ) => {
        match_cfg! {
            #[cfg($cfg0)] => { $($i)* }
        }
        $(
            match_cfg! {
                #[cfg($cfgs)] @ #[cfg($cfg0)] => { $($is)* }
            }
        )*
    };
    (
        $(#[cfg($cfgs:meta)] => { $($is:item)* })*
        _ => { $($ni:item)* }
    ) => {
        match_cfg! {
            $( #[cfg($cfgs)] => { $($is)* } )*
        }
        match_cfg! {
            _ @ #[cfg(any($($cfgs),*))] => { $($ni)* }
        }
    };
}

#[cfg(test)]
mod tests {
    match_cfg! {
        #[cfg(target_pointer_width = "64")] => { fn f0_() -> bool { true }}
    }
    match_cfg! {
        #[cfg(unix)] => { fn f1_() -> bool { true }}
        #[cfg(any(target_os = "macos", target_os = "linux"))] => { fn f1_() -> bool { false }}
    }

    match_cfg! {
        #[cfg(target_pointer_width = "64")] => { fn f2_() -> bool { true }}
        #[cfg(target_pointer_width = "32")] => { fn f2_() -> bool { false }}
    }

    match_cfg! {
        #[cfg(target_pointer_width = "8")] => { fn f3_() -> i32 { 0 }}
        #[cfg(target_pointer_width = "16")] => { fn f3_() -> i32 { 1 }}
        _ => { fn f3_() -> i32 { 2 }}
    }

    #[test]
    fn tests() {
        #[cfg(target_pointer_width = "64")]
        {
            assert!(f0_());
        }
        #[cfg(unix)]
        {
            assert!(f1_());
        }
        assert!(f2_());
        assert_eq!(f3_(), 2);
    }

    match_cfg! {
        #[cfg(test)] => {
            use core::option::Option as Option2;
            fn works1() -> Option2<u32> { Some(1) }
        }
        _ => {
            fn works1() -> Option<u32> { None }
        }
    }

    match_cfg! {
        #[cfg(foo)] => {
            fn works2() -> bool { false }
        }
        #[cfg(test)] => {
            fn works2() -> bool { true }
        }
        _ => {
            fn works2() -> bool { false }
        }
    }

    match_cfg! {
        #[cfg(foo)] => {
            fn works3() -> bool { false }
        }
        _ => {
            fn works3() -> bool { true }
        }
    }

    match_cfg! {
        #[cfg(test)] => {
            use core::option::Option as Option3;
            fn works4() -> Option3<u32> { Some(1) }
        }
    }

    match_cfg! {
        #[cfg(foo)] => {
            fn works5() -> bool { false }
        }
        #[cfg(test)] => {
            fn works5() -> bool { true }
        }
    }

    #[test]
    fn it_works() {
        assert!(works1().is_some());
        assert!(works2());
        assert!(works3());
        assert!(works4().is_some());
        assert!(works5());
    }
}