@@ -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,89 +187,80 @@ 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
cfg_if ! {
154
191
if #[ cfg( feature = "const-extern-fn" ) ] {
192
+ /// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
155
193
macro_rules! f {
156
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
157
- $( $arg: ident: $argty: ty) , *
158
- ) -> $ret: ty {
159
- $( $body: stmt) ; *
160
- } ) * ) => ( $(
194
+ ( $(
195
+ $( #[ $attr: meta] ) *
196
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
197
+ $( $body: stmt) ; *
198
+ }
199
+ ) * ) => ( $(
161
200
#[ inline]
162
201
$( #[ $attr] ) *
163
- pub $( $constness) * unsafe extern fn $i( $( $arg: $argty) , *
164
- ) -> $ret {
202
+ pub $( $constness) * unsafe extern fn $i( $( $arg: $argty) , * ) -> $ret {
165
203
$( $body) ; *
166
204
}
167
205
) * )
168
206
}
169
207
208
+ /// Define a safe function that is const as long as `const-extern-fn` is enabled.
170
209
macro_rules! safe_f {
171
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
172
- $( $arg: ident: $argty: ty) , *
173
- ) -> $ret: ty {
174
- $( $body: stmt) ; *
175
- } ) * ) => ( $(
210
+ ( $(
211
+ $( #[ $attr: meta] ) *
212
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
213
+ $( $body: stmt) ; *
214
+ }
215
+ ) * ) => ( $(
176
216
#[ inline]
177
217
$( #[ $attr] ) *
178
- pub $( $constness) * extern fn $i( $( $arg: $argty) , *
179
- ) -> $ret {
218
+ pub $( $constness) * extern fn $i( $( $arg: $argty) , * ) -> $ret {
180
219
$( $body) ; *
181
220
}
182
221
) * )
183
222
}
184
223
224
+ /// A nonpublic function that is const as long as `const-extern-fn` is enabled.
185
225
macro_rules! const_fn {
186
- ( $( $( #[ $attr: meta] ) * $( { $constness: ident} ) * fn $i: ident(
187
- $( $arg: ident: $argty: ty) , *
188
- ) -> $ret: ty {
189
- $( $body: stmt) ; *
190
- } ) * ) => ( $(
226
+ ( $(
227
+ $( #[ $attr: meta] ) *
228
+ $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
229
+ $( $body: stmt) ; *
230
+ }
231
+ ) * ) => ( $(
191
232
#[ inline]
192
233
$( #[ $attr] ) *
193
- $( $constness) * fn $i( $( $arg: $argty) , *
194
- ) -> $ret {
234
+ $( $constness) * fn $i( $( $arg: $argty) , * ) -> $ret {
195
235
$( $body) ; *
196
236
}
197
237
) * )
198
238
}
199
-
200
239
} else {
240
+ /// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
201
241
macro_rules! f {
202
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
203
- $( $arg: ident: $argty: ty) , *
204
- ) -> $ret: ty {
205
- $( $body: stmt) ; *
206
- } ) * ) => ( $(
242
+ ( $(
243
+ $( #[ $attr: meta] ) *
244
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
245
+ $( $body: stmt) ; *
246
+ }
247
+ ) * ) => ( $(
207
248
#[ inline]
208
249
$( #[ $attr] ) *
209
- pub unsafe extern fn $i( $( $arg: $argty) , *
210
- ) -> $ret {
250
+ pub unsafe extern fn $i( $( $arg: $argty) , * ) -> $ret {
211
251
$( $body) ; *
212
252
}
213
253
) * )
214
254
}
215
255
256
+ /// Define a safe function that is const as long as `const-extern-fn` is enabled.
216
257
macro_rules! safe_f {
217
- ( $( $( #[ $attr: meta] ) * pub $( { $constness: ident} ) * fn $i: ident(
218
- $( $arg: ident: $argty: ty) , *
219
- ) -> $ret: ty {
220
- $( $body: stmt) ; *
221
- } ) * ) => ( $(
258
+ ( $(
259
+ $( #[ $attr: meta] ) *
260
+ pub $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
261
+ $( $body: stmt) ; *
262
+ }
263
+ ) * ) => ( $(
222
264
#[ inline]
223
265
$( #[ $attr] ) *
224
266
pub extern fn $i( $( $arg: $argty) , *
@@ -228,16 +270,17 @@ cfg_if! {
228
270
) * )
229
271
}
230
272
273
+ /// A nonpublic function that is const as long as `const-extern-fn` is enabled.
231
274
macro_rules! const_fn {
232
- ( $( $( #[ $attr: meta] ) * $( { $constness: ident} ) * fn $i: ident(
233
- $( $arg: ident: $argty: ty) , *
234
- ) -> $ret: ty {
235
- $( $body: stmt) ; *
236
- } ) * ) => ( $(
275
+ ( $(
276
+ $( #[ $attr: meta] ) *
277
+ $( { $constness: ident} ) * fn $i: ident( $( $arg: ident: $argty: ty) , * ) -> $ret: ty {
278
+ $( $body: stmt) ; *
279
+ }
280
+ ) * ) => ( $(
237
281
#[ inline]
238
282
$( #[ $attr] ) *
239
- fn $i( $( $arg: $argty) , *
240
- ) -> $ret {
283
+ fn $i( $( $arg: $argty) , * ) -> $ret {
241
284
$( $body) ; *
242
285
}
243
286
) * )
0 commit comments