#cfg #macro #numbers #matching #structured #gets #ergonomically

cfg-if

A macro to ergonomically define an item depending on a large number of #[cfg] parameters. Structured like an if-else chain, the first matching branch is the item that gets emitted.

12 releases (1 stable)

1.0.0 Oct 6, 2020
0.1.10 Sep 24, 2019
0.1.9 May 15, 2019
0.1.7 Mar 4, 2019
0.1.0 Jul 8, 2015

#35 in Rust patterns

Download history 2608618/week @ 2023-12-04 2600842/week @ 2023-12-11 2252602/week @ 2023-12-18 1483902/week @ 2023-12-25 2071844/week @ 2024-01-01 2507185/week @ 2024-01-08 2500582/week @ 2024-01-15 2532627/week @ 2024-01-22 2779267/week @ 2024-01-29 2649735/week @ 2024-02-05 2648638/week @ 2024-02-12 2773590/week @ 2024-02-19 2873266/week @ 2024-02-26 2794397/week @ 2024-03-04 2703002/week @ 2024-03-11 2743750/week @ 2024-03-18

11,317,856 downloads per month
Used in 66,262 crates (1,628 directly)

MIT/Apache

9KB
118 lines

cfg-if

Documentation

A macro to ergonomically define an item depending on a large number of #[cfg] parameters. Structured like an if-else chain, the first matching branch is the item that gets emitted.

[dependencies]
cfg-if = "0.1"

Example

cfg_if::cfg_if! {
    if #[cfg(unix)] {
        fn foo() { /* unix specific functionality */ }
    } else if #[cfg(target_pointer_width = "32")] {
        fn foo() { /* non-unix, 32-bit functionality */ }
    } else {
        fn foo() { /* fallback implementation */ }
    }
}

fn main() {
    foo();
}

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in cfg-if by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~200KB