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