-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fdf279e
Showing
7 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: build | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
|
||
- name: fail scenarios | ||
run: | | ||
cargo run --example main 2>&1 | grep -q 'exactly one' | ||
cargo run --example main --features "a","b" 2>&1 | grep -q 'mutually exclusive' | ||
- name: pass scenarios | ||
run: | | ||
cargo run --example main --features "a" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "mutually_exclusive_features" | ||
version = "0.0.1" | ||
description = "Macros to check that only none or one of a set of features is enabled at a time, as known as mutually exclusive features" | ||
license = "MIT OR Apache-2.0" | ||
authors = ["Omid Rad <[email protected]>"] | ||
repository = "https://github.com/omid/mutually_exclusive_features" | ||
keywords = ["feature", "macro", "mutually_exclusive", "mutually", "exclusive"] | ||
categories = ["development-tools", "no-std", "rust-patterns"] | ||
edition = "2021" | ||
rust-version = "1.56.0" | ||
|
||
[features] | ||
a = [] | ||
b = [] | ||
|
||
[[example]] | ||
name = "main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Omid Rad | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Some macros to check mutually exclusive conditions of features in Rust. | ||
|
||
The macros are `none_or_one_of` and `exactly_one_of`. | ||
|
||
Both check mutually exclusive of 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. | ||
|
||
To use `none_or_one_of`, call it with the list of features you want to be mutually exclusive: | ||
```rust | ||
use mutually_exclusive_features::none_or_one_of; | ||
none_or_one_of!("feature1", "feature2", "feature3"); | ||
``` | ||
|
||
Which will generate the following code: | ||
```rust | ||
#[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!"); | ||
``` | ||
|
||
And `exactly_one_of`, is 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: | ||
```rust | ||
#[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!"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use mutually_exclusive_features::{exactly_one_of, none_or_one_of}; | ||
|
||
fn main() { | ||
none_or_one_of!("a", "b", "c"); | ||
none_or_one_of!("a", "b",); | ||
none_or_one_of!("a"); | ||
none_or_one_of!(); | ||
|
||
exactly_one_of!("a", "b", "c"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#![no_std] | ||
|
||
/** | ||
This macro allows you to specify that certain features are mutually exclusive. | ||
It generates any combination of the selected features, by the combination formula: | ||
`n!/(2!*(n - 2)!)`, where n is the number of features. | ||
For example, if you have 5 features, you will get 5!/(3!*2!) which is 10 combinations | ||
or for 10 features, you will get 10!/(8!*2!) which is 45 combinations | ||
To use it, simply call it with the 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: | ||
```ignore | ||
#[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!"); | ||
``` | ||
**/ | ||
#[macro_export] | ||
macro_rules! none_or_one_of { | ||
// root call | ||
($($F:literal),+ $(,)?) => { | ||
none_or_one_of!(@recurs: [params: $($F,)*]); | ||
}; | ||
|
||
// exactly two | ||
( | ||
@recurs: | ||
[params: $F1:literal, $F2:literal,] | ||
) => { | ||
#[cfg(all(feature=$F1, feature=$F2))] | ||
compile_error!(concat!("The `", $F1, "` and `", $F2, "` features are mutually exclusive and cannot be enabled at the same time!")); | ||
}; | ||
|
||
// more than two | ||
( | ||
@recurs: | ||
[params: $F1:literal, $($FS:literal,)+] | ||
) => { | ||
$(none_or_one_of!(@recurs: [params: $F1, $FS,]);)* | ||
none_or_one_of!(@recurs: [params: $($FS,)*]); | ||
}; | ||
|
||
// ignore cases | ||
() => {}; | ||
(@recurs: [params: $F1:literal,]) => {}; | ||
} | ||
|
||
/** | ||
This is exactly like `none_or_one_of` except that it requires exactly one of the features to be selected. | ||
To use it, simply call it with the features you want to be mutually exclusive: | ||
``` | ||
use mutually_exclusive_features::exactly_one_of; | ||
exactly_one_of!("feature1", "feature2", "feature3"); | ||
``` | ||
Which will generate the following code: | ||
```ignore | ||
#[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!"); | ||
``` | ||
**/ | ||
#[macro_export] | ||
macro_rules! exactly_one_of { | ||
($($F:literal),+ $(,)?) => { | ||
none_or_one_of!(@recurs: [params: $($F,)*]); | ||
|
||
#[cfg(not(any($(feature=$F),*)))] | ||
compile_error!(concat!("You must enable exactly one of ", exactly_one_of!(@comma_sep: $($F,)*), " features!")); | ||
}; | ||
|
||
(@comma_sep: $last:literal $(,)?) => { concat!("`", $last, "`") }; | ||
(@comma_sep: $head:literal, $($rest:literal),+ $(,)?) => { | ||
concat!("`", $head, "`, ", exactly_one_of!(@comma_sep: $($rest),+)) | ||
}; | ||
} |