#exclusive #features #macro #mutually #mutually-exclusive

no-std mutually_exclusive_features

Macros to check that only none or one of a set of features is enabled at a time, as known as mutually exclusive features

2 unstable releases

0.1.0 Feb 5, 2024
0.0.3 Nov 9, 2023
0.0.2 Nov 9, 2023
0.0.1 Nov 9, 2023

#421 in Rust patterns

Download history 35161/week @ 2024-07-23 31976/week @ 2024-07-30 33106/week @ 2024-08-06 29234/week @ 2024-08-13 30093/week @ 2024-08-20 29257/week @ 2024-08-27 33060/week @ 2024-09-03 32409/week @ 2024-09-10 34831/week @ 2024-09-17 34065/week @ 2024-09-24 36782/week @ 2024-10-01 33997/week @ 2024-10-08 40031/week @ 2024-10-15 37279/week @ 2024-10-22 34148/week @ 2024-10-29 35134/week @ 2024-11-05

152,856 downloads per month
Used in 54 crates (5 directly)

MIT/Apache

8KB

Mutually Exclusive Features

crates.io Build Status docs

Macros to check mutually exclusive features in Rust


It contains none_or_one_of and exactly_one_of macros.

Both check mutually exclusive features in Rust, but none_or_one_of allows for no features to be enabled, while exactly_one_of requires exactly one feature to be enabled.

Usage


none_or_one_of


Call it with the list of features you want to be mutually exclusive:

use mutually_exclusive_features::none_or_one_of;
none_or_one_of!("feature1", "feature2", "feature3");

Which will generate the following code:

#[cfg(all(feature="feature1", feature="feature2"))]
compile_error!("The `feature1` and `feature2` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature1", feature="feature3"))]
compile_error!("The `feature1` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature2", feature="feature3"))]
compile_error!("The `feature2` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");

exactly_one_of


It's the same, but requires exactly one feature to be enabled:

use mutually_exclusive_features::exactly_one_of;
exactly_one_of!("feature1", "feature2", "feature3");

Which will generate the following code:

#[cfg(all(feature="feature1", feature="feature2"))]
compile_error!("The `feature1` and `feature2` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature1", feature="feature3"))]
compile_error!("The `feature1` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(all(feature="feature2", feature="feature3"))]
compile_error!("The `feature2` and `feature3` features are mutually exclusive and cannot be enabled at the same time!");

#[cfg(not(any(feature="feature1", feature="feature2", feature="feature3")))]
compile_error!("You must enable exactly one of `feature1`, `feature2`, `feature3` features!");

No runtime deps