1 | // See the cfg-if crate. |
2 | #[allow (unused_macro_rules)] |
3 | macro_rules! cfg_if { |
4 | // match if/else chains with a final `else` |
5 | ($( |
6 | if #[cfg($($meta:meta),*)] { $($it:item)* } |
7 | ) else * else { |
8 | $($it2:item)* |
9 | }) => { |
10 | cfg_if! { |
11 | @__items |
12 | () ; |
13 | $( ( ($($meta),*) ($($it)*) ), )* |
14 | ( () ($($it2)*) ), |
15 | } |
16 | }; |
17 | |
18 | // match if/else chains lacking a final `else` |
19 | ( |
20 | if #[cfg($($i_met:meta),*)] { $($i_it:item)* } |
21 | $( |
22 | else if #[cfg($($e_met:meta),*)] { $($e_it:item)* } |
23 | )* |
24 | ) => { |
25 | cfg_if! { |
26 | @__items |
27 | () ; |
28 | ( ($($i_met),*) ($($i_it)*) ), |
29 | $( ( ($($e_met),*) ($($e_it)*) ), )* |
30 | ( () () ), |
31 | } |
32 | }; |
33 | |
34 | // Internal and recursive macro to emit all the items |
35 | // |
36 | // Collects all the negated cfgs in a list at the beginning and after the |
37 | // semicolon is all the remaining items |
38 | (@__items ($($not:meta,)*) ; ) => {}; |
39 | (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => { |
40 | // Emit all items within one block, applying an approprate #[cfg]. The |
41 | // #[cfg] will require all `$m` matchers specified and must also negate |
42 | // all previous matchers. |
43 | cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* } |
44 | |
45 | // Recurse to emit all other items in `$rest`, and when we do so add all |
46 | // our `$m` matchers to the list of `$not` matchers as future emissions |
47 | // will have to negate everything we just matched as well. |
48 | cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* } |
49 | }; |
50 | |
51 | // Internal macro to Apply a cfg attribute to a list of items |
52 | (@__apply $m:meta, $($it:item)*) => { |
53 | $(#[$m] $it)* |
54 | }; |
55 | } |
56 | |
57 | // Helper macro for specialization. This also helps avoid parse errors if the |
58 | // default fn syntax for specialization changes in the future. |
59 | #[cfg (feature = "nightly" )] |
60 | macro_rules! default_fn { |
61 | (#[$($a:tt)*] $($tt:tt)*) => { |
62 | #[$($a)*] default $($tt)* |
63 | } |
64 | } |
65 | #[cfg (not(feature = "nightly" ))] |
66 | macro_rules! default_fn { |
67 | ($($tt:tt)*) => { |
68 | $($tt)* |
69 | } |
70 | } |
71 | |