1 /// A macro for defining #[cfg] if-else statements. 2 /// 3 /// This is similar to the `if/elif` C preprocessor macro by allowing definition 4 /// of a cascade of `#[cfg]` cases, emitting the implementation which matches 5 /// first. 6 /// 7 /// This allows you to conveniently provide a long list #[cfg]'d blocks of code 8 /// without having to rewrite each clause multiple times. 9 #[allow(unused_macros)] 10 macro_rules! cfg_if { 11 // match if/else chains with a final `else` 12 ($( 13 if #[cfg($($meta:meta),*)] { $($it:item)* } 14 ) else * else { 15 $($it2:item)* 16 }) => { 17 cfg_if! { 18 @__items 19 () ; 20 $( ( ($($meta),*) ($($it)*) ), )* 21 ( () ($($it2)*) ), 22 } 23 }; 24 25 // match if/else chains lacking a final `else` 26 ( 27 if #[cfg($($i_met:meta),*)] { $($i_it:item)* } 28 $( 29 else if #[cfg($($e_met:meta),*)] { $($e_it:item)* } 30 )* 31 ) => { 32 cfg_if! { 33 @__items 34 () ; 35 ( ($($i_met),*) ($($i_it)*) ), 36 $( ( ($($e_met),*) ($($e_it)*) ), )* 37 ( () () ), 38 } 39 }; 40 41 // Internal and recursive macro to emit all the items 42 // 43 // Collects all the negated cfgs in a list at the beginning and after the 44 // semicolon is all the remaining items 45 (@__items ($($not:meta,)*) ; ) => {}; 46 (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), 47 $($rest:tt)*) => { 48 // Emit all items within one block, applying an approprate #[cfg]. The 49 // #[cfg] will require all `$m` matchers specified and must also negate 50 // all previous matchers. 51 cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* } 52 53 // Recurse to emit all other items in `$rest`, and when we do so add all 54 // our `$m` matchers to the list of `$not` matchers as future emissions 55 // will have to negate everything we just matched as well. 56 cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* } 57 }; 58 59 // Internal macro to Apply a cfg attribute to a list of items 60 (@__apply $m:meta, $($it:item)*) => { 61 $(#[$m] $it)* 62 }; 63 } 64 65 #[allow(unused_macros)] 66 macro_rules! s { 67 ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($( 68 s!(it: $(#[$attr])* pub $t $i { $($field)* }); 69 )*); 70 (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => ( 71 compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead"); 72 ); 73 (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => ( 74 __item! { 75 #[repr(C)] 76 #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))] 77 #[allow(deprecated)] 78 $(#[$attr])* 79 pub struct $i { $($field)* } 80 } 81 #[allow(deprecated)] 82 impl ::Copy for $i {} 83 #[allow(deprecated)] 84 impl ::Clone for $i { 85 fn clone(&self) -> $i { *self } 86 } 87 ); 88 } 89 90 #[allow(unused_macros)] 91 macro_rules! s_no_extra_traits { 92 ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($( 93 s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* }); 94 )*); 95 (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => ( 96 cfg_if! { 97 if #[cfg(libc_union)] { 98 __item! { 99 #[repr(C)] 100 $(#[$attr])* 101 pub union $i { $($field)* } 102 } 103 104 impl ::Copy for $i {} 105 impl ::Clone for $i { 106 fn clone(&self) -> $i { *self } 107 } 108 } 109 } 110 ); 111 (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => ( 112 __item! { 113 #[repr(C)] 114 $(#[$attr])* 115 pub struct $i { $($field)* } 116 } 117 impl ::Copy for $i {} 118 impl ::Clone for $i { 119 fn clone(&self) -> $i { *self } 120 } 121 ); 122 } 123 124 #[allow(unused_macros)] 125 macro_rules! f { 126 ($(pub fn $i:ident($($arg:ident: $argty:ty),*) -> $ret:ty { 127 $($body:stmt);* 128 })*) => ($( 129 #[inline] 130 pub unsafe extern fn $i($($arg: $argty),*) -> $ret { 131 $($body);* 132 } 133 )*) 134 } 135 136 #[allow(unused_macros)] 137 macro_rules! __item { 138 ($i:item) => { 139 $i 140 }; 141 } 142 143 #[allow(unused_macros)] 144 macro_rules! align_const { 145 ($($(#[$attr:meta])* 146 pub const $name:ident : $t1:ty 147 = $t2:ident { $($field:tt)* };)*) => ($( 148 #[cfg(libc_align)] 149 $(#[$attr])* 150 pub const $name : $t1 = $t2 { 151 $($field)* 152 }; 153 #[cfg(not(libc_align))] 154 $(#[$attr])* 155 pub const $name : $t1 = $t2 { 156 $($field)* 157 __align: [], 158 }; 159 )*) 160 } 161 162 // This macro is used to deprecate items that should be accessed via the mach crate 163 #[allow(unused_macros)] 164 macro_rules! deprecated_mach { 165 (pub const $id:ident: $ty:ty = $expr:expr;) => { 166 #[deprecated( 167 since = "0.2.55", 168 note = "Use the `mach` crate instead", 169 )] 170 #[allow(deprecated)] 171 pub const $id: $ty = $expr; 172 }; 173 ($(pub const $id:ident: $ty:ty = $expr:expr;)*) => { 174 $( 175 deprecated_mach!( 176 pub const $id: $ty = $expr; 177 ); 178 )* 179 }; 180 (pub type $id:ident = $ty:ty;) => { 181 #[deprecated( 182 since = "0.2.55", 183 note = "Use the `mach` crate instead", 184 )] 185 #[allow(deprecated)] 186 pub type $id = $ty; 187 }; 188 ($(pub type $id:ident = $ty:ty;)*) => { 189 $( 190 deprecated_mach!( 191 pub type $id = $ty; 192 ); 193 )* 194 } 195 } 196