#macro #cfg #numbers #chain #branch #parameters #define

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

#39 in Rust patterns

Download history 3573778/week @ 2024-08-10 3482986/week @ 2024-08-17 3837803/week @ 2024-08-24 3462232/week @ 2024-08-31 3763171/week @ 2024-09-07 3247177/week @ 2024-09-14 3746412/week @ 2024-09-21 3984475/week @ 2024-09-28 4527888/week @ 2024-10-05 4725155/week @ 2024-10-12 4139788/week @ 2024-10-19 3474151/week @ 2024-10-26 3707714/week @ 2024-11-02 3663521/week @ 2024-11-09 3893188/week @ 2024-11-16 3249980/week @ 2024-11-23

15,038,998 downloads per month
Used in 80,611 crates (1,955 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

~205KB