@@ -61,13 +61,23 @@ macro_rules! cfg_if {
61
61
} ;
62
62
}
63
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.
64
69
macro_rules! s {
65
- ( $( $( #[ $attr: meta] ) * pub $t: ident $i: ident { $( $field: tt) * } ) * ) => ( $(
70
+ ( $(
71
+ $( #[ $attr: meta] ) *
72
+ pub $t: ident $i: ident { $( $field: tt) * }
73
+ ) * ) => ( $(
66
74
s!( it: $( #[ $attr] ) * pub $t $i { $( $field) * } ) ;
67
75
) * ) ;
76
+
68
77
( it: $( #[ $attr: meta] ) * pub union $i: ident { $( $field: tt) * } ) => (
69
78
compile_error!( "unions cannot derive extra traits, use s_no_extra_traits instead" ) ;
70
79
) ;
80
+
71
81
( it: $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
72
82
__item! {
73
83
#[ repr( C ) ]
@@ -85,10 +95,38 @@ macro_rules! s {
85
95
) ;
86
96
}
87
97
98
+ /// Implement `Clone` and `Copy` for a tuple struct, as well as `Debug`, `Eq`, `Hash`,
99
+ /// and `PartialEq` if the `extra_traits` feature is enabled.
100
+ ///
101
+ /// This is the same as [`s`] but works for tuple structs.
102
+ macro_rules! s_paren {
103
+ ( $(
104
+ $( #[ $attr: meta] ) *
105
+ pub struct $i: ident ( $( $field: tt) * ) ;
106
+ ) * ) => ( $(
107
+ __item! {
108
+ #[ cfg_attr( feature = "extra_traits" , derive( Debug , Eq , Hash , PartialEq ) ) ]
109
+ $( #[ $attr] ) *
110
+ pub struct $i ( $( $field) * ) ;
111
+ }
112
+ impl :: Copy for $i { }
113
+ impl :: Clone for $i {
114
+ fn clone( & self ) -> $i { * self }
115
+ }
116
+ ) * ) ;
117
+ }
118
+
119
+ /// Implement `Clone` and `Copy` for a struct with no `extra_traits` feature.
120
+ ///
121
+ /// Most items will prefer to use [`s`].
88
122
macro_rules! s_no_extra_traits {
89
- ( $( $( #[ $attr: meta] ) * pub $t: ident $i: ident { $( $field: tt) * } ) * ) => ( $(
123
+ ( $(
124
+ $( #[ $attr: meta] ) *
125
+ pub $t: ident $i: ident { $( $field: tt) * }
126
+ ) * ) => ( $(
90
127
s_no_extra_traits!( it: $( #[ $attr] ) * pub $t $i { $( $field) * } ) ;
91
128
) * ) ;
129
+
92
130
( it: $( #[ $attr: meta] ) * pub union $i: ident { $( $field: tt) * } ) => (
93
131
__item! {
94
132
#[ repr( C ) ]
@@ -101,6 +139,7 @@ macro_rules! s_no_extra_traits {
101
139
fn clone( & self ) -> $i { * self }
102
140
}
103
141
) ;
142
+
104
143
( it: $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
105
144
__item! {
106
145
#[ repr( C ) ]
@@ -116,14 +155,26 @@ macro_rules! s_no_extra_traits {
116
155
) ;
117
156
}
118
157
158
+ /// Specify that an enum should have no traits that aren't specified in the macro
159
+ /// invocation, i.e. no `Clone` or `Copy`.
119
160
macro_rules! missing {
120
- ( $( $( #[ $attr: meta] ) * pub enum $i: ident { } ) * ) => ( $(
121
- $( #[ $attr] ) * #[ allow( missing_copy_implementations) ] pub enum $i { }
161
+ ( $(
162
+ $( #[ $attr: meta] ) *
163
+ pub enum $i: ident { }
164
+ ) * ) => ( $(
165
+ $( #[ $attr] ) *
166
+ #[ allow( missing_copy_implementations) ]
167
+ pub enum $i { }
122
168
) * ) ;
123
169
}
124
170
171
+ /// Implement `Clone` and `Copy` for an enum, as well as `Debug`, `Eq`, `Hash`, and
172
+ /// `PartialEq` if the `extra_traits` feature is enabled.
125
173
macro_rules! e {
126
- ( $( $( #[ $attr: meta] ) * pub enum $i: ident { $( $field: tt) * } ) * ) => ( $(
174
+ ( $(
175
+ $( #[ $attr: meta] ) *
176
+ pub enum $i: ident { $( $field: tt) * }
177
+ ) * ) => ( $(
127
178
__item! {
128
179
#[ cfg_attr( feature = "extra_traits" , derive( Debug , Eq , Hash , PartialEq ) ) ]
129
180
$( #[ $attr] ) *
@@ -136,20 +187,6 @@ macro_rules! e {
136
187
) * ) ;
137
188
}
138
189
139
- macro_rules! s_paren {
140
- ( $( $( #[ $attr: meta] ) * pub struct $i: ident ( $( $field: tt) * ) ; ) * ) => ( $(
141
- __item! {
142
- #[ cfg_attr( feature = "extra_traits" , derive( Debug , Eq , Hash , PartialEq ) ) ]
143
- $( #[ $attr] ) *
144
- pub struct $i ( $( $field) * ) ;
145
- }
146
- impl :: Copy for $i { }
147
- impl :: Clone for $i {
148
- fn clone( & self ) -> $i { * self }
149
- }
150
- ) * ) ;
151
- }
152
-
153
190
// This is a pretty horrible hack to allow us to conditionally mark
154
191
// some functions as 'const', without requiring users of this macro
155
192
// to care about the "const-extern-fn" feature.
@@ -179,92 +216,97 @@ macro_rules! s_paren {
179
216
// 'f!' block
180
217
cfg_if ! {
181
218
if #[ cfg( libc_const_extern_fn) ] {
219
+ /// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
182
220
macro_rules! f {
183
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
184
- $( $arg: ident: $argty: ty) , *
185
- ) -> $ret: ty {
186
- $( $body: stmt) ; *
187
- } ) * ) => ( $(
221
+ ( $(
222
+ $( #[ $attr: meta] ) *
223
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
224
+ $( $body: stmt) ; *
225
+ }
226
+ ) * ) => ( $(
188
227
#[ inline]
189
228
$( #[ $attr] ) *
190
- pub $( $constness) * unsafe extern fn $i( $( $arg: $argty) , *
191
- ) -> $ret {
229
+ pub $( $constness) * unsafe extern fn $i( $( $arg: $argty) , * ) -> $ret {
192
230
$( $body) ; *
193
231
}
194
232
) * )
195
233
}
196
234
235
+ /// Define a safe function that is const as long as `const-extern-fn` is enabled.
197
236
macro_rules! safe_f {
198
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
199
- $( $arg: ident: $argty: ty) , *
200
- ) -> $ret: ty {
201
- $( $body: stmt) ; *
202
- } ) * ) => ( $(
237
+ ( $(
238
+ $( #[ $attr: meta] ) *
239
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
240
+ $( $body: stmt) ; *
241
+ }
242
+ ) * ) => ( $(
203
243
#[ inline]
204
244
$( #[ $attr] ) *
205
- pub $( $constness) * extern fn $i( $( $arg: $argty) , *
206
- ) -> $ret {
245
+ pub $( $constness) * extern fn $i( $( $arg: $argty) , * ) -> $ret {
207
246
$( $body) ; *
208
247
}
209
248
) * )
210
249
}
211
250
251
+ /// A nonpublic function that is const as long as `const-extern-fn` is enabled.
212
252
macro_rules! const_fn {
213
- ( $( $( #[ $attr: meta] ) * $( { $constness: ident} ) * fn $i: ident(
214
- $( $arg: ident: $argty: ty) , *
215
- ) -> $ret: ty {
216
- $( $body: stmt) ; *
217
- } ) * ) => ( $(
253
+ ( $(
254
+ $( #[ $attr: meta] ) *
255
+ $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
256
+ $( $body: stmt) ; *
257
+ }
258
+ ) * ) => ( $(
218
259
#[ inline]
219
260
$( #[ $attr] ) *
220
- $( $constness) * fn $i( $( $arg: $argty) , *
221
- ) -> $ret {
261
+ $( $constness) * fn $i( $( $arg: $argty) , * ) -> $ret {
222
262
$( $body) ; *
223
263
}
224
264
) * )
225
265
}
226
-
227
266
} else {
267
+ /// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
228
268
macro_rules! f {
229
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
230
- $( $arg: ident: $argty: ty) , *
231
- ) -> $ret: ty {
232
- $( $body: stmt) ; *
233
- } ) * ) => ( $(
269
+ ( $(
270
+ $( #[ $attr: meta] ) *
271
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
272
+ $( $body: stmt) ; *
273
+ }
274
+ ) * ) => ( $(
234
275
#[ inline]
235
276
$( #[ $attr] ) *
236
- pub unsafe extern fn $i( $( $arg: $argty) , *
237
- ) -> $ret {
277
+ pub unsafe extern fn $i( $( $arg: $argty) , * ) -> $ret {
238
278
$( $body) ; *
239
279
}
240
280
) * )
241
281
}
242
282
283
+ /// Define a safe function that is const as long as `const-extern-fn` is enabled.
243
284
macro_rules! safe_f {
244
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
245
- $( $arg: ident: $argty: ty) , *
246
- ) -> $ret: ty {
247
- $( $body: stmt) ; *
248
- } ) * ) => ( $(
285
+ ( $(
286
+ $( #[ $attr: meta] ) *
287
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
288
+ $( $body: stmt) ; *
289
+ }
290
+ ) * ) => ( $(
249
291
#[ inline]
250
292
$( #[ $attr] ) *
251
- pub extern fn $i( $( $arg: $argty) , *
252
- ) -> $ret {
293
+ pub extern fn $i( $( $arg: $argty) , * ) -> $ret {
253
294
$( $body) ; *
254
295
}
255
296
) * )
256
297
}
257
298
299
+ /// A nonpublic function that is const as long as `const-extern-fn` is enabled.
258
300
macro_rules! const_fn {
259
- ( $( $( #[ $attr: meta] ) * $( { $constness: ident} ) * fn $i: ident(
260
- $( $arg: ident: $argty: ty) , *
261
- ) -> $ret: ty {
262
- $( $body: stmt) ; *
263
- } ) * ) => ( $(
301
+ ( $(
302
+ $( #[ $attr: meta] ) *
303
+ $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
304
+ $( $body: stmt) ; *
305
+ }
306
+ ) * ) => ( $(
264
307
#[ inline]
265
308
$( #[ $attr] ) *
266
- fn $i( $( $arg: $argty) , *
267
- ) -> $ret {
309
+ fn $i( $( $arg: $argty) , * ) -> $ret {
268
310
$( $body) ; *
269
311
}
270
312
) * )
0 commit comments