[][src]Macro match_cfg::match_cfg

macro_rules! match_cfg {
    (#[cfg($cfg:meta)] => { $($i:item)* }) => { ... };
    (#[cfg($cfg:meta)] @ #[cfg($cfg_not:meta)] => { $($i:item)* }) => { ... };
    (_ => { $($i:item)* }) => { ... };
    (_ @ #[cfg($cfg_not:meta)] => { $($i:item)* }) => { ... };
    (
        #[cfg($cfg0:meta)] => { $($i:item)* }
        $(#[cfg($cfgs:meta)] => { $($is:item)* })*
    ) => { ... };
    (
        $(#[cfg($cfgs:meta)] => { $($is:item)* })*
        _ => { $($ni:item)* }
    ) => { ... };
}

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 */ }
    }
}