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 macro_rules! cfg_if { 10 // match if/else chains with a final `else` 11 ($( 12 if #[cfg($($meta:meta),*)] { $($it:item)* } 13 ) else * else { 14 $($it2:item)* 15 }) => { 16 cfg_if! { 17 @__items 18 () ; 19 $( ( ($($meta),*) ($($it)*) ), )* 20 ( () ($($it2)*) ), 21 } 22 }; 23 24 // match if/else chains lacking a final `else` 25 ( 26 if #[cfg($($i_met:meta),*)] { $($i_it:item)* } 27 $( 28 else if #[cfg($($e_met:meta),*)] { $($e_it:item)* } 29 )* 30 ) => { 31 cfg_if! { 32 @__items 33 () ; 34 ( ($($i_met),*) ($($i_it)*) ), 35 $( ( ($($e_met),*) ($($e_it)*) ), )* 36 ( () () ), 37 } 38 }; 39 40 // Internal and recursive macro to emit all the items 41 // 42 // Collects all the negated `cfg`s in a list at the beginning and after the 43 // semicolon is all the remaining items 44 (@__items ($($not:meta,)*) ; ) => {}; 45 (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), 46 $($rest:tt)*) => { 47 // Emit all items within one block, applying an appropriate #[cfg]. The 48 // #[cfg] will require all `$m` matchers specified and must also negate 49 // all previous matchers. 50 cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* } 51 52 // Recurse to emit all other items in `$rest`, and when we do so add all 53 // our `$m` matchers to the list of `$not` matchers as future emissions 54 // will have to negate everything we just matched as well. 55 cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* } 56 }; 57 58 // Internal macro to Apply a cfg attribute to a list of items 59 (@__apply $m:meta, $($it:item)*) => { 60 $(#[$m] $it)* 61 }; 62 } 63 64 /// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and 65 /// `PartialEq` if the `extra_traits` feature is enabled. 66 /// 67 /// Use [`s_no_extra_traits`] for structs where the `extra_traits` feature does not 68 /// make sense, and for unions. 69 macro_rules! s { 70 ($( 71 $(#[$attr:meta])* 72 pub $t:ident $i:ident { $($field:tt)* } 73 )*) => ($( 74 s!(it: $(#[$attr])* pub $t $i { $($field)* }); 75 )*); 76 77 (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => ( 78 compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead"); 79 ); 80 81 (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => ( 82 __item! { 83 #[repr(C)] 84 #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))] 85 #[derive(Copy, Clone)] 86 #[allow(deprecated)] 87 $(#[$attr])* 88 pub struct $i { $($field)* } 89 } 90 ); 91 } 92 93 /// Implement `Clone` and `Copy` for a tuple struct, as well as `Debug`, `Eq`, `Hash`, 94 /// and `PartialEq` if the `extra_traits` feature is enabled. 95 /// 96 /// This is the same as [`s`] but works for tuple structs. 97 macro_rules! s_paren { 98 ($( 99 $(#[$attr:meta])* 100 pub struct $i:ident ( $($field:tt)* ); 101 )*) => ($( 102 __item! { 103 #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))] 104 #[derive(Copy, Clone)] 105 $(#[$attr])* 106 pub struct $i ( $($field)* ); 107 } 108 )*); 109 } 110 111 /// Implement `Clone` and `Copy` for a struct with no `extra_traits` feature. 112 /// 113 /// Most items will prefer to use [`s`]. 114 macro_rules! s_no_extra_traits { 115 ($( 116 $(#[$attr:meta])* 117 pub $t:ident $i:ident { $($field:tt)* } 118 )*) => ($( 119 s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* }); 120 )*); 121 122 (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => ( 123 __item! { 124 #[repr(C)] 125 #[derive(Copy, Clone)] 126 $(#[$attr])* 127 pub union $i { $($field)* } 128 } 129 ); 130 131 (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => ( 132 __item! { 133 #[repr(C)] 134 #[derive(Copy, Clone)] 135 $(#[$attr])* 136 pub struct $i { $($field)* } 137 } 138 ); 139 } 140 141 /// Specify that an enum should have no traits that aren't specified in the macro 142 /// invocation, i.e. no `Clone` or `Copy`. 143 macro_rules! missing { 144 ($( 145 $(#[$attr:meta])* 146 pub enum $i:ident {} 147 )*) => ($( 148 $(#[$attr])* 149 #[allow(missing_copy_implementations)] 150 pub enum $i { } 151 )*); 152 } 153 154 /// Implement `Clone` and `Copy` for an enum, as well as `Debug`, `Eq`, `Hash`, and 155 /// `PartialEq` if the `extra_traits` feature is enabled. 156 macro_rules! e { 157 ($( 158 $(#[$attr:meta])* 159 pub enum $i:ident { $($field:tt)* } 160 )*) => ($( 161 __item! { 162 #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))] 163 #[derive(Copy, Clone)] 164 $(#[$attr])* 165 pub enum $i { $($field)* } 166 } 167 )*); 168 } 169 170 // This is a pretty horrible hack to allow us to conditionally mark some functions as 'const', 171 // without requiring users of this macro to care "libc_const_extern_fn". 172 // 173 // When 'libc_const_extern_fn' is enabled, we emit the captured 'const' keyword in the expanded 174 // function. 175 // 176 // When 'libc_const_extern_fn' is disabled, we always emit a plain 'pub unsafe extern fn'. 177 // Note that the expression matched by the macro is exactly the same - this allows 178 // users of this macro to work whether or not 'libc_const_extern_fn' is enabled 179 // 180 // Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks. 181 // This is because 'const unsafe extern fn' won't even parse on older compilers, 182 // so we need to avoid emitting it at all of 'libc_const_extern_fn'. 183 // 184 // Specifically, moving the 'cfg_if' into the macro body will *not* work. Doing so would cause the 185 // '#[cfg(libc_const_extern_fn)]' to be emitted into user code. The 'cfg' gate will not stop Rust 186 // from trying to parse the 'pub const unsafe extern fn', so users would get a compiler error even 187 // when the 'libc_const_extern_fn' feature is disabled. 188 189 // FIXME(ctest): ctest can't handle `const extern` functions, we should be able to remove this 190 // cfg completely. 191 // FIXME(ctest): ctest can't handle `$(,)?` so we use `$(,)*` which isn't quite correct. 192 cfg_if! { 193 if #[cfg(libc_const_extern_fn)] { 194 /// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled. 195 macro_rules! f { 196 ($( 197 $(#[$attr:meta])* 198 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty 199 $body:block 200 )*) => ($( 201 #[inline] 202 $(#[$attr])* 203 pub $($constness)* unsafe extern fn $i($($arg: $argty),*) -> $ret 204 $body 205 )*) 206 } 207 208 /// Define a safe function that is const as long as `libc_const_extern_fn` is enabled. 209 macro_rules! safe_f { 210 ($( 211 $(#[$attr:meta])* 212 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty 213 $body:block 214 )*) => ($( 215 #[inline] 216 $(#[$attr])* 217 pub $($constness)* extern fn $i($($arg: $argty),*) -> $ret 218 $body 219 )*) 220 } 221 222 /// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled. 223 macro_rules! const_fn { 224 ($( 225 $(#[$attr:meta])* 226 $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty 227 $body:block 228 )*) => ($( 229 #[inline] 230 $(#[$attr])* 231 $($constness)* fn $i($($arg: $argty),*) -> $ret 232 $body 233 )*) 234 } 235 } else { 236 /// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled. 237 macro_rules! f { 238 ($( 239 $(#[$attr:meta])* 240 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty 241 $body:block 242 )*) => ($( 243 #[inline] 244 $(#[$attr])* 245 pub unsafe extern fn $i($($arg: $argty),*) -> $ret 246 $body 247 )*) 248 } 249 250 /// Define a safe function that is const as long as `libc_const_extern_fn` is enabled. 251 macro_rules! safe_f { 252 ($( 253 $(#[$attr:meta])* 254 pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty 255 $body:block 256 )*) => ($( 257 #[inline] 258 $(#[$attr])* 259 pub extern fn $i($($arg: $argty),*) -> $ret 260 $body 261 )*) 262 } 263 264 /// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled. 265 macro_rules! const_fn { 266 ($( 267 $(#[$attr:meta])* 268 $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty 269 $body:block 270 )*) => ($( 271 #[inline] 272 $(#[$attr])* 273 fn $i($($arg: $argty),*) -> $ret 274 $body 275 )*) 276 } 277 } 278 } 279 280 macro_rules! __item { 281 ($i:item) => { 282 $i 283 }; 284 } 285 286 // This macro is used to deprecate items that should be accessed via the mach2 crate 287 macro_rules! deprecated_mach { 288 (pub const $id:ident: $ty:ty = $expr:expr;) => { 289 #[deprecated( 290 since = "0.2.55", 291 note = "Use the `mach2` crate instead", 292 )] 293 #[allow(deprecated)] 294 pub const $id: $ty = $expr; 295 }; 296 ($(pub const $id:ident: $ty:ty = $expr:expr;)*) => { 297 $( 298 deprecated_mach!( 299 pub const $id: $ty = $expr; 300 ); 301 )* 302 }; 303 (pub type $id:ident = $ty:ty;) => { 304 #[deprecated( 305 since = "0.2.55", 306 note = "Use the `mach2` crate instead", 307 )] 308 #[allow(deprecated)] 309 pub type $id = $ty; 310 }; 311 ($(pub type $id:ident = $ty:ty;)*) => { 312 $( 313 deprecated_mach!( 314 pub type $id = $ty; 315 ); 316 )* 317 } 318 } 319