Skip to content
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

convert to token trees #28

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Changes from 2 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
44 changes: 31 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@
macro_rules! cfg_if {
// match if/else chains with a final `else`
($(
if #[cfg($($meta:meta),*)] { $($it:item)* }
if #[cfg($($meta:meta),*)] { $($tokens:tt)* }
) else * else {
$($it2:item)*
$($tokens2:tt)*
}) => {
$crate::cfg_if! {
@__items
() ;
$( ( ($($meta),*) ($($it)*) ), )*
( () ($($it2)*) ),
$( ( ($($meta),*) ($($tokens)*) ), )*
( () ($($tokens2)*) ),
}
};

// match if/else chains lacking a final `else`
(
if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
if #[cfg($($i_met:meta),*)] { $($i_tokens:tt)* }
$(
else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
else if #[cfg($($e_met:meta),*)] { $($e_tokens:tt)* }
)*
) => {
$crate::cfg_if! {
@__items
() ;
( ($($i_met),*) ($($i_it)*) ),
$( ( ($($e_met),*) ($($e_it)*) ), )*
( ($($i_met),*) ($($i_tokens)*) ),
$( ( ($($e_met),*) ($($e_tokens)*) ), )*
( () () ),
}
};
Expand All @@ -67,11 +67,11 @@ macro_rules! cfg_if {
// Collects all the negated cfgs in a list at the beginning and after the
// semicolon is all the remaining items
(@__items ($($not:meta,)*) ; ) => {};
(@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
// Emit all items within one block, applying an approprate #[cfg]. The
(@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($tokens:tt)*) ), $($rest:tt)*) => {
// Emit all items within one block, applying an appropriate #[cfg]. The
// #[cfg] will require all `$m` matchers specified and must also negate
// all previous matchers.
$crate::cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }
$crate::cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($tokens)* }
Lokathor marked this conversation as resolved.
Show resolved Hide resolved

// Recurse to emit all other items in `$rest`, and when we do so add all
// our `$m` matchers to the list of `$not` matchers as future emissions
Expand All @@ -80,8 +80,14 @@ macro_rules! cfg_if {
};

// Internal macro to Apply a cfg attribute to a list of items
(@__apply $m:meta, $($it:item)*) => {
$(#[$m] $it)*
(@__apply $m:meta, $($tokens:tt)*) => {
#[$m] $crate::cfg_if! { @__identity $($tokens)* }
};

// Internal macro to make __apply work out right for different match types,
// because of how macros matching/expand stuff.
(@__identity $($tokens:tt)*) => {
$($tokens)*
};
}

Expand Down Expand Up @@ -137,4 +143,16 @@ mod tests {
assert!(works4().is_some());
assert!(works5());
}

extern crate std;
Lokathor marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn test_usage_within_a_function() {
cfg_if! {if #[cfg(debug_assertions)] {
std::println!("debug");
std::println!("debug2");
} else {
std::println!("release");
std::println!("release2");
}}
}
}