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