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