#[doc(hidden)]
#[cfg(not(cfg_select))]
macro_rules! cfg_select {
(
@parsed($($clauses:meta),*)
_ => $final_arm:expr $(,)?
) => {
#[cfg(not(any($($clauses),+)))]
{
$final_arm
}
};
(
@parsed($($clauses:meta),*)
$cfg:meta => $final_arm:expr $(,)?
) => {
#[cfg(all($cfg, not(any($($clauses),*))))]
{
$final_arm
}
#[cfg(not(any($($clauses,)* $cfg)))]
compile_error!(
"cfg_select! requires a final `_ => expr` arm to be used as a fallback when no other cfg matches"
);
};
(
@parsed($($clauses:meta),*)
$cfg:meta => $arm:expr, $($rest:tt)*
) => {
#[cfg(all($cfg, not(any($($clauses),*))))]
{
$arm
}
cfg_select! {
@parsed($($clauses,)* $cfg)
$($rest)*
}
};
(
@parsed($($clauses:meta),*)
$cfg:meta => $arm:block $($rest:tt)*
) => {
#[cfg(all($cfg, not(any($($clauses),*))))]
$arm
cfg_select! {
@parsed($($clauses,)* $cfg)
$($rest)*
}
};
(
$cfg:meta => $($rest:tt)*
) => {
{
cfg_select! {
@parsed()
$cfg => $($rest)*
}
}
};
}
#[cfg(test)]
mod tests {
#[test]
#[cfg(not(cfg_select))]
fn test_cfg_select_polyfill_short_circuit() {
cfg_select! {
all() => {},
all() => {
unreachable!("the first arm should be selected, so this arm should not be evaluated");
}
}
}
}