cfg_match

This commit is contained in:
nora 2022-10-23 22:30:08 +02:00
parent 8fadf72227
commit 785b3952b9
No known key found for this signature in database
3 changed files with 60 additions and 2 deletions

2
Cargo.lock generated
View file

@ -3,5 +3,5 @@
version = 3
[[package]]
name = "haha"
name = "uwu"
version = "0.1.0"

View file

@ -1,5 +1,5 @@
[package]
name = "haha"
name = "uwu"
version = "0.1.0"
edition = "2021"

View file

@ -0,0 +1,58 @@
#[macro_export]
macro_rules! cfg_match {
() => {};
(_ => { $($tt:tt)* }) => {
$($tt)*
};
(
$head_pattern:meta => { $($head_body:tt)* }
$($rest:tt)*
) => {
#[cfg($head_pattern)]
$crate::cfg_match! { _ => { $($head_body)* } }
#[cfg(not($head_pattern))]
$crate::cfg_match! {
$($rest)*
}
};
}
#[cfg(test)]
mod tests {
#[test]
fn correct_one_selected() {
crate::cfg_match! {
any() => {
panic!();
}
all() => {
}
any() => {
panic!();
}
}
}
#[test]
fn underscore() {
crate::cfg_match! {
_ => {}
}
}
#[test]
fn fallback() {
crate::cfg_match! {
any() => {
panic!();
}
any() => {
panic!();
}
_ => {}
}
}
}