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