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.
9macro_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
64macro_rules! s {
65 ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
66 s!(it: $(#[$attr])* pub $t $i { $($field)* });
67 )*);
68 (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
69 compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead");
70 );
71 (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
72 __item! {
73 #[repr(C)]
74 #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
75 #[allow(deprecated)]
76 $(#[$attr])*
77 pub struct $i { $($field)* }
78 }
79 #[allow(deprecated)]
80 impl ::Copy for $i {}
81 #[allow(deprecated)]
82 impl ::Clone for $i {
83 fn clone(&self) -> $i { *self }
84 }
85 );
86}
87
88macro_rules! s_no_extra_traits {
89 ($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
90 s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
91 )*);
92 (it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
93 cfg_if! {
94 if #[cfg(libc_union)] {
95 __item! {
96 #[repr(C)]
97 $(#[$attr])*
98 pub union $i { $($field)* }
99 }
100
101 impl ::Copy for $i {}
102 impl ::Clone for $i {
103 fn clone(&self) -> $i { *self }
104 }
105 }
106 }
107 );
108 (it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
109 __item! {
110 #[repr(C)]
111 $(#[$attr])*
112 pub struct $i { $($field)* }
113 }
114 #[allow(deprecated)]
115 impl ::Copy for $i {}
116 #[allow(deprecated)]
117 impl ::Clone for $i {
118 fn clone(&self) -> $i { *self }
119 }
120 );
121}
122
123macro_rules! missing {
124 ($($(#[$attr:meta])* pub enum $i:ident {})*) => ($(
125 $(#[$attr])* #[allow(missing_copy_implementations)] pub enum $i { }
126 )*);
127}
128
129macro_rules! e {
130 ($($(#[$attr:meta])* pub enum $i:ident { $($field:tt)* })*) => ($(
131 __item! {
132 #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
133 $(#[$attr])*
134 pub enum $i { $($field)* }
135 }
136 impl ::Copy for $i {}
137 impl ::Clone for $i {
138 fn clone(&self) -> $i { *self }
139 }
140 )*);
141}
142
143macro_rules! s_paren {
144 ($($(#[$attr:meta])* pub struct $i:ident ( $($field:tt)* ); )* ) => ($(
145 __item! {
146 #[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
147 $(#[$attr])*
148 pub struct $i ( $($field)* );
149 }
150 impl ::Copy for $i {}
151 impl ::Clone for $i {
152 fn clone(&self) -> $i { *self }
153 }
154 )*);
155}
156
157// This is a pretty horrible hack to allow us to conditionally mark
158// some functions as 'const', without requiring users of this macro
159// to care about the "const-extern-fn" feature.
160//
161// When 'const-extern-fn' is enabled, we emit the captured 'const' keyword
162// in the expanded function.
163//
164// When 'const-extern-fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
165// Note that the expression matched by the macro is exactly the same - this allows
166// users of this macro to work whether or not 'const-extern-fn' is enabled
167//
168// Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks.
169// This is because 'const unsafe extern fn' won't even parse on older compilers,
170// so we need to avoid emitting it at all of 'const-extern-fn'.
171//
172// Specifically, moving the 'cfg_if' into the macro body will *not* work.
173// Doing so would cause the '#[cfg(feature = "const-extern-fn")]' to be emitted
174// into user code. The 'cfg' gate will not stop Rust from trying to parse the
175// 'pub const unsafe extern fn', so users would get a compiler error even when
176// the 'const-extern-fn' feature is disabled
177//
178// Note that users of this macro need to place 'const' in a weird position
179// (after the closing ')' for the arguments, but before the return type).
180// This was the only way I could satisfy the following two requirements:
181// 1. Avoid ambiguity errors from 'macro_rules!' (which happen when writing '$foo:ident fn'
182// 2. Allow users of this macro to mix 'pub fn foo' and 'pub const fn bar' within the same
183// 'f!' block
184cfg_if! {
185 if #[cfg(libc_const_extern_fn)] {
186 macro_rules! f {
187 ($($(#[$attr:meta])* pub $({$constness:ident})* fn $i:ident(
188 $($arg:ident: $argty:ty),*
189 ) -> $ret:ty {
190 $($body:stmt);*
191 })*) => ($(
192 #[inline]
193 $(#[$attr])*
194 pub $($constness)* unsafe extern fn $i($($arg: $argty),*
195 ) -> $ret {
196 $($body);*
197 }
198 )*)
199 }
200
201 macro_rules! safe_f {
202 ($($(#[$attr:meta])* pub $({$constness:ident})* fn $i:ident(
203 $($arg:ident: $argty:ty),*
204 ) -> $ret:ty {
205 $($body:stmt);*
206 })*) => ($(
207 #[inline]
208 $(#[$attr])*
209 pub $($constness)* extern fn $i($($arg: $argty),*
210 ) -> $ret {
211 $($body);*
212 }
213 )*)
214 }
215
216 macro_rules! const_fn {
217 ($($(#[$attr:meta])* $({$constness:ident})* fn $i:ident(
218 $($arg:ident: $argty:ty),*
219 ) -> $ret:ty {
220 $($body:stmt);*
221 })*) => ($(
222 #[inline]
223 $(#[$attr])*
224 $($constness)* fn $i($($arg: $argty),*
225 ) -> $ret {
226 $($body);*
227 }
228 )*)
229 }
230
231 } else {
232 macro_rules! f {
233 ($($(#[$attr:meta])* pub $({$constness:ident})* fn $i:ident(
234 $($arg:ident: $argty:ty),*
235 ) -> $ret:ty {
236 $($body:stmt);*
237 })*) => ($(
238 #[inline]
239 $(#[$attr])*
240 pub unsafe extern fn $i($($arg: $argty),*
241 ) -> $ret {
242 $($body);*
243 }
244 )*)
245 }
246
247 macro_rules! safe_f {
248 ($($(#[$attr:meta])* pub $({$constness:ident})* fn $i:ident(
249 $($arg:ident: $argty:ty),*
250 ) -> $ret:ty {
251 $($body:stmt);*
252 })*) => ($(
253 #[inline]
254 $(#[$attr])*
255 pub extern fn $i($($arg: $argty),*
256 ) -> $ret {
257 $($body);*
258 }
259 )*)
260 }
261
262 macro_rules! const_fn {
263 ($($(#[$attr:meta])* $({$constness:ident})* fn $i:ident(
264 $($arg:ident: $argty:ty),*
265 ) -> $ret:ty {
266 $($body:stmt);*
267 })*) => ($(
268 #[inline]
269 $(#[$attr])*
270 fn $i($($arg: $argty),*
271 ) -> $ret {
272 $($body);*
273 }
274 )*)
275 }
276 }
277}
278
279macro_rules! __item {
280 ($i:item) => {
281 $i
282 };
283}
284
285macro_rules! align_const {
286 ($($(#[$attr:meta])*
287 pub const $name:ident : $t1:ty
288 = $t2:ident { $($field:tt)* };)*) => ($(
289 #[cfg(libc_align)]
290 $(#[$attr])*
291 pub const $name : $t1 = $t2 {
292 $($field)*
293 };
294 #[cfg(not(libc_align))]
295 $(#[$attr])*
296 pub const $name : $t1 = $t2 {
297 $($field)*
298 __align: [],
299 };
300 )*)
301}
302
303// This macro is used to deprecate items that should be accessed via the mach2 crate
304macro_rules! deprecated_mach {
305 (pub const $id:ident: $ty:ty = $expr:expr;) => {
306 #[deprecated(
307 since = "0.2.55",
308 note = "Use the `mach2` crate instead",
309 )]
310 #[allow(deprecated)]
311 pub const $id: $ty = $expr;
312 };
313 ($(pub const $id:ident: $ty:ty = $expr:expr;)*) => {
314 $(
315 deprecated_mach!(
316 pub const $id: $ty = $expr;
317 );
318 )*
319 };
320 (pub type $id:ident = $ty:ty;) => {
321 #[deprecated(
322 since = "0.2.55",
323 note = "Use the `mach2` crate instead",
324 )]
325 #[allow(deprecated)]
326 pub type $id = $ty;
327 };
328 ($(pub type $id:ident = $ty:ty;)*) => {
329 $(
330 deprecated_mach!(
331 pub type $id = $ty;
332 );
333 )*
334 }
335}
336
337#[cfg(not(libc_ptr_addr_of))]
338macro_rules! ptr_addr_of {
339 ($place:expr) => {
340 &$place
341 };
342}
343
344#[cfg(libc_ptr_addr_of)]
345macro_rules! ptr_addr_of {
346 ($place:expr) => {
347 ::core::ptr::addr_of!($place)
348 };
349}
350