cfg_match!() { /* proc-macro */ }
Expand description
Matches against various cfg settings.
§Examples
use osom_lib_cfg_ext::cfg_match;
cfg_match!(
(target_os = "windows") => {
fn os_name() -> &'static str {
"W"
}
},
(target_os = "linux") => {
fn os_name() -> &'static str {
"L"
}
},
_ => {
fn os_name() -> &'static str {
"U"
}
}
);
The call above will generate only one os_name
function, depending on the matched
condition. The condition is translated to one of #[cfg(...)]
attributes.
Conditions are evaluated one after another. In particular the order doesn’t matter if they are independent. However it does matter in general. For example say we have this:
use osom_lib_cfg_ext::cfg_match;
cfg_match!(
(A) => {
const A: i32 = 1;
},
(B) => {
const B: i32 = 2;
}
);
Then if B
implies A
(as a condition) then B
arm will never be matched here.
For it to be matched you need to reverse the order:
use osom_lib_cfg_ext::cfg_match;
cfg_match!(
(B) => {
const B: i32 = 2;
},
(A) => {
const A: i32 = 1;
}
);