Skip to content

Commit 33a8869

Browse files
authored
Merge pull request #28 from Lokathor/token-trees
convert to token trees
2 parents f71c600 + 571a28d commit 33a8869

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/lib.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,30 @@
3434
macro_rules! cfg_if {
3535
// match if/else chains with a final `else`
3636
($(
37-
if #[cfg($($meta:meta),*)] { $($it:item)* }
37+
if #[cfg($($meta:meta),*)] { $($tokens:tt)* }
3838
) else * else {
39-
$($it2:item)*
39+
$($tokens2:tt)*
4040
}) => {
4141
$crate::cfg_if! {
4242
@__items
4343
() ;
44-
$( ( ($($meta),*) ($($it)*) ), )*
45-
( () ($($it2)*) ),
44+
$( ( ($($meta),*) ($($tokens)*) ), )*
45+
( () ($($tokens2)*) ),
4646
}
4747
};
4848

4949
// match if/else chains lacking a final `else`
5050
(
51-
if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
51+
if #[cfg($($i_met:meta),*)] { $($i_tokens:tt)* }
5252
$(
53-
else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
53+
else if #[cfg($($e_met:meta),*)] { $($e_tokens:tt)* }
5454
)*
5555
) => {
5656
$crate::cfg_if! {
5757
@__items
5858
() ;
59-
( ($($i_met),*) ($($i_it)*) ),
60-
$( ( ($($e_met),*) ($($e_it)*) ), )*
59+
( ($($i_met),*) ($($i_tokens)*) ),
60+
$( ( ($($e_met),*) ($($e_tokens)*) ), )*
6161
( () () ),
6262
}
6363
};
@@ -67,21 +67,22 @@ macro_rules! cfg_if {
6767
// Collects all the negated cfgs in a list at the beginning and after the
6868
// semicolon is all the remaining items
6969
(@__items ($($not:meta,)*) ; ) => {};
70-
(@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
71-
// Emit all items within one block, applying an approprate #[cfg]. The
70+
(@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($tokens:tt)*) ), $($rest:tt)*) => {
71+
// Emit all items within one block, applying an appropriate #[cfg]. The
7272
// #[cfg] will require all `$m` matchers specified and must also negate
7373
// all previous matchers.
74-
$crate::cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }
74+
#[cfg(all($($m,)* not(any($($not),*))))] $crate::cfg_if! { @__identity $($tokens)* }
7575

7676
// Recurse to emit all other items in `$rest`, and when we do so add all
7777
// our `$m` matchers to the list of `$not` matchers as future emissions
7878
// will have to negate everything we just matched as well.
7979
$crate::cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
8080
};
8181

82-
// Internal macro to Apply a cfg attribute to a list of items
83-
(@__apply $m:meta, $($it:item)*) => {
84-
$(#[$m] $it)*
82+
// Internal macro to make __apply work out right for different match types,
83+
// because of how macros matching/expand stuff.
84+
(@__identity $($tokens:tt)*) => {
85+
$($tokens)*
8586
};
8687
}
8788

@@ -137,4 +138,18 @@ mod tests {
137138
assert!(works4().is_some());
138139
assert!(works5());
139140
}
141+
142+
#[test]
143+
#[allow(clippy::assertions_on_constants)]
144+
fn test_usage_within_a_function() {
145+
cfg_if! {if #[cfg(debug_assertions)] {
146+
// we want to put more than one thing here to make sure that they
147+
// all get configured properly.
148+
assert!(cfg!(debug_assertions));
149+
assert_eq!(4, 2+2);
150+
} else {
151+
assert!(works1().is_some());
152+
assert_eq!(10, 5+5);
153+
}}
154+
}
140155
}

0 commit comments

Comments
 (0)