[][src]Macro cfg_match::cfg_match

macro_rules! cfg_match {
    (_ => { $($i:item)* }) => { ... };
    (_ => $(#[$m:meta])+ $i:item) => { ... };
    (_ => $e:expr$(,)?) => { ... };
    (_ => $i:item) => { ... };
    ($cfg:meta => { $($i:item)* } $($t:tt)*) => { ... };
    ($cfg:meta => $(#[$m:meta])+ $i:item $($t:tt)*) => { ... };
    ($cfg:meta => $e:expr, $($t:tt)*) => { ... };
    ($cfg:meta => $i:item $($t:tt)*) => { ... };
    (#[cfg(_)] $i:item $($t:tt)*) => { ... };
    (#[cfg($cfg:meta)] $i:item $($t:tt)*) => { ... };
    (@not(_) #[cfg('do)] $i:item $($t:tt)*) => { ... };
    (@not(_) #[cfg(_)] $i:item $($t:tt)*) => { ... };
    (@not(_)) => { ... };
    (@not($not:meta)) => { ... };
    (@not($cfg:meta) #[cfg('do)] $i:item $($t:tt)*) => { ... };
    (@not($not:meta) $($t:tt)*) => { ... };
    () => { ... };
}

cfg_match! provides a more ergonomic approach to chaining conditionals, like the similar cfg-if crate. In addition to items, cfg_match! can also be used for expressions (though a block will require parenthesis wrapping like => ({ ... })). Compile-time conditionals

The macro stops at the first matching branch, just like a traditional match:


cfg_match! {
    feature = "foo" => {
        fn bar() {
            println!("have foo");
        }
    }
    _ =>
        fn bar() {
            println!("no foo :(");
        }
}

Alternatively, the above can be written as:


cfg_match! {
    #[cfg(feature = "foo")]
    /// Does a thing because of foo.
    fn bar() {
        println!("have foo");
    }

    #[cfg(_)]
    fn bar() {
        println!("no foo :(");
    }
}