Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ members = ["proc"]
nightly = []
proc = ["proc_static_assertions"]

# These are used for doctests in assert_one_feature
feature-1 = []
feature-2 = []

default = ["feature-1", "feature-2"]

[badges]
maintenance = { status = "actively-maintained" }
39 changes: 39 additions & 0 deletions src/assert_one_feature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// Asserts that exactly one feature of a set is enabled.
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// // Both feature-1 and feature-2 are set, so this fails
///
/// assert_one_feature!("feature-1","foo","bar","feature-2");
/// ```
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// // None of these features are set, so this fails
///
/// assert_one_feature!("foo", "bar", "baz");
/// ```
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// // Exactly one of these features ("feature-1") is set, so this is fine
///
/// assert_one_feature!("foo", "bar", "baz", "qup", "feature-1");
/// assert_one_feature!("foo", "bar", "baz", "qup", "feature-2");
/// ```
#[macro_export]
macro_rules! assert_one_feature {
() => {};
($($feature:literal),+) => {
#[cfg(not(any($(
feature = $feature,
)+)))]
compile_error!(concat!("Must have one of the following features: ", stringify!($($feature,)+)));
const _: () = {
$(
#[cfg(feature = $feature)]
const ASSERT_ONE_FEATURE:() = ();
)+
};
};
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ mod assert_cfg;
mod assert_fields;
mod assert_impl;
mod assert_obj_safe;
mod assert_one_feature;
mod assert_size;
mod assert_trait;
mod assert_type;
Expand Down