Skip to content

Allow to have multiple conditions in the same macro call #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
87 changes: 86 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@
//!
//! # fn main() {}
//! ```
//!
//! You can have other `if` conditions in the same macro call:
//!
//! ```
//! cfg_if::cfg_if! {
//! if #[cfg(unix)] {
//! fn foo() { /* unix specific functionality */ }
//! }
//! if #[cfg(feature = "bar")] {
//! fn bar() {}
//! }
//! }
//!
//! # fn main() {}
//! ```

#![no_std]
#![doc(html_root_url = "https://docs.rs/cfg-if")]
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
#![cfg_attr(test, deny(warnings), allow(unexpected_cfgs))]

/// The main macro provided by this crate. See crate documentation for more
/// information.
Expand All @@ -48,6 +63,26 @@ macro_rules! cfg_if {
}
};

// Allow to multiple conditions in a same call.
(
$(
if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
) else+
else { $( $e_tokens:tt )* }
if $($extra_conditions:tt)+
) => {
$crate::cfg_if! {
@__items () ;
$(
(( $i_meta ) ( $( $i_tokens )* )) ,
)+
(() ( $( $e_tokens )* )) ,
}
$crate::cfg_if! {
if $($extra_conditions)+
}
};

// match if/else chains lacking a final `else`
(
if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
Expand All @@ -64,6 +99,26 @@ macro_rules! cfg_if {
}
};

// Allow to multiple conditions in a same call.
(
if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
$(
else if #[cfg( $e_meta:meta )] { $( $e_tokens:tt )* }
)*
if $($extra_conditions:tt)+
) => {
$crate::cfg_if! {
@__items () ;
(( $i_meta ) ( $( $i_tokens )* )) ,
$(
(( $e_meta ) ( $( $e_tokens )* )) ,
)*
}
$crate::cfg_if! {
if $($extra_conditions)+
}
};

// Internal and recursive macro to emit all the items
//
// Collects all the previous cfgs in a list at the beginning, so they can be
Expand Down Expand Up @@ -143,13 +198,43 @@ mod tests {
}
}

cfg_if! {
if #[cfg(foo)] {
fn works6() -> bool { false }
} else if #[cfg(test)] {
fn works6() -> bool { true }
}
if #[cfg(test)] {
fn works7() -> bool { true }
} else {
fn works7() -> bool { false }
}
}

cfg_if! {
if #[cfg(test)] {
fn works8() -> bool { true }
} else if #[cfg(foo)] {
fn works8() -> bool { false }
}
if #[cfg(foo)] {
fn works9() -> bool { false }
} else if #[cfg(test)] {
fn works9() -> bool { true }
}
}

#[test]
fn it_works() {
assert!(works1().is_some());
assert!(works2());
assert!(works3());
assert!(works4().is_some());
assert!(works5());
assert!(works6());
assert!(works7());
assert!(works8());
assert!(works9());
}

#[test]
Expand Down