Skip to content

Commit 1f76862

Browse files
committed
Add assert_one_feature! macro
1 parent a867e97 commit 1f76862

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ members = ["proc"]
2424
nightly = []
2525
proc = ["proc_static_assertions"]
2626

27+
# These are used for doctests in assert_one_feature
28+
feature-1 = []
29+
feature-2 = []
30+
31+
default = ["feature-1", "feature-2"]
32+
2733
[badges]
2834
maintenance = { status = "actively-maintained" }

src/assert_one_feature.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// Asserts that exactly one feature of a set is enabled.
2+
///
3+
/// ```compile_fail
4+
/// # #[macro_use] extern crate static_assertions; fn main() {}
5+
/// // Both feature-1 and feature-2 are set, so this fails
6+
///
7+
/// assert_one_feature!("feature-1","foo","bar","feature-2");
8+
/// ```
9+
///
10+
/// ```compile_fail
11+
/// # #[macro_use] extern crate static_assertions; fn main() {}
12+
/// // None of these features are set, so this fails
13+
///
14+
/// assert_one_feature!("foo", "bar", "baz");
15+
/// ```
16+
///
17+
/// ```
18+
/// # #[macro_use] extern crate static_assertions; fn main() {}
19+
/// // Exactly one of these features ("feature-1") is set, so this is fine
20+
///
21+
/// assert_one_feature!("foo", "bar", "baz", "qup", "feature-1");
22+
/// ```
23+
#[macro_export]
24+
macro_rules! assert_one_feature {
25+
() => {};
26+
($($feature:literal),+) => {
27+
#[cfg(not(any($(
28+
feature = $feature,
29+
)+)))]
30+
compile_error!(concat!("Must have one of the following features: ", stringify!($($feature,)+)));
31+
$(
32+
#[cfg(feature = $feature)]
33+
const ASSERT_ONE_FEATURE:() = ();
34+
)+
35+
};
36+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ mod assert_cfg;
116116
mod assert_fields;
117117
mod assert_impl;
118118
mod assert_obj_safe;
119+
mod assert_one_feature;
119120
mod assert_size;
120121
mod assert_trait;
121122
mod assert_type;

0 commit comments

Comments
 (0)