Currently, #[cfg()]
provides following predicate options: any()
, all()
and not()
, which is unfortunately not enough when dealing with packages where only single feature must be enabled.
For example, if you have a package that must enable only single feature from following list:
("s112", "s113", "s122", "s132", "s140")
, you'll first need check if at least one is enabled, and then create a O(n^2) loop to see if any two features are not enabled in parallel:
#[cfg(not(any(
feature = "s112",
feature = "s113",
feature = "s122",
feature = "s132",
feature = "s140"
)))]
compile_error!("No softdevice feature activated. You must activate exactly one of the following features: s112, s113, s122, s132, s140");
#[cfg(any(
all(feature = "s112", feature = "s113"),
all(feature = "s112", feature = "s122"),
all(feature = "s112", feature = "s132"),
all(feature = "s112", feature = "s140"),
all(feature = "s113", feature = "s122"),
all(feature = "s113", feature = "s132"),
all(feature = "s113", feature = "s140"),
all(feature = "s122", feature = "s132"),
all(feature = "s122", feature = "s140"),
all(feature = "s132", feature = "s140"),
))]
compile_error!("Multiple softdevice features activated. You must activate exactly one of the following features: s112, s113, s122, s132, s140");
This could be alleviated by introducing one()
predicate that would return true if single item in the list of features provided is available.
#[cfg(not(one(
feature = "s112",
feature = "s113",
feature = "s122",
feature = "s132",
feature = "s140"
)))]
compile_error!("Only one of the following features should be enabled: s112, s113, s122, s132, s140");
For more gruesome examples where having one()
would help can be found here: nrf-softdevice/nrf-softdevice/src/lib.rs at 77e4f26b426fa854b878fd27cfb46cb88bfc8b95 · embassy-rs/nrf-softdevice · GitHub