Skip to content

Commit 8a4aedb

Browse files
committed
Make macros.rs more similar to what is on main
1 parent bd40454 commit 8a4aedb

File tree

1 file changed

+104
-62
lines changed

1 file changed

+104
-62
lines changed

src/macros.rs

Lines changed: 104 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,23 @@ macro_rules! cfg_if {
6161
};
6262
}
6363

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.
6469
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+
)*) => ($(
6674
s!(it: $(#[$attr])* pub $t $i { $($field)* });
6775
)*);
76+
6877
(it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
6978
compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead");
7079
);
80+
7181
(it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
7282
__item! {
7383
#[repr(C)]
@@ -85,10 +95,38 @@ macro_rules! s {
8595
);
8696
}
8797

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`].
88122
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+
)*) => ($(
90127
s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
91128
)*);
129+
92130
(it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
93131
__item! {
94132
#[repr(C)]
@@ -101,6 +139,7 @@ macro_rules! s_no_extra_traits {
101139
fn clone(&self) -> $i { *self }
102140
}
103141
);
142+
104143
(it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
105144
__item! {
106145
#[repr(C)]
@@ -116,14 +155,26 @@ macro_rules! s_no_extra_traits {
116155
);
117156
}
118157

158+
/// Specify that an enum should have no traits that aren't specified in the macro
159+
/// invocation, i.e. no `Clone` or `Copy`.
119160
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 { }
122168
)*);
123169
}
124170

171+
/// Implement `Clone` and `Copy` for an enum, as well as `Debug`, `Eq`, `Hash`, and
172+
/// `PartialEq` if the `extra_traits` feature is enabled.
125173
macro_rules! e {
126-
($($(#[$attr:meta])* pub enum $i:ident { $($field:tt)* })*) => ($(
174+
($(
175+
$(#[$attr:meta])*
176+
pub enum $i:ident { $($field:tt)* }
177+
)*) => ($(
127178
__item! {
128179
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
129180
$(#[$attr])*
@@ -136,20 +187,6 @@ macro_rules! e {
136187
)*);
137188
}
138189

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-
153190
// This is a pretty horrible hack to allow us to conditionally mark
154191
// some functions as 'const', without requiring users of this macro
155192
// to care about the "const-extern-fn" feature.
@@ -179,92 +216,97 @@ macro_rules! s_paren {
179216
// 'f!' block
180217
cfg_if! {
181218
if #[cfg(libc_const_extern_fn)] {
219+
/// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
182220
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+
)*) => ($(
188227
#[inline]
189228
$(#[$attr])*
190-
pub $($constness)* unsafe extern fn $i($($arg: $argty),*
191-
) -> $ret {
229+
pub $($constness)* unsafe extern fn $i($($arg: $argty),*) -> $ret {
192230
$($body);*
193231
}
194232
)*)
195233
}
196234

235+
/// Define a safe function that is const as long as `const-extern-fn` is enabled.
197236
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+
)*) => ($(
203243
#[inline]
204244
$(#[$attr])*
205-
pub $($constness)* extern fn $i($($arg: $argty),*
206-
) -> $ret {
245+
pub $($constness)* extern fn $i($($arg: $argty),*) -> $ret {
207246
$($body);*
208247
}
209248
)*)
210249
}
211250

251+
/// A nonpublic function that is const as long as `const-extern-fn` is enabled.
212252
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+
)*) => ($(
218259
#[inline]
219260
$(#[$attr])*
220-
$($constness)* fn $i($($arg: $argty),*
221-
) -> $ret {
261+
$($constness)* fn $i($($arg: $argty),*) -> $ret {
222262
$($body);*
223263
}
224264
)*)
225265
}
226-
227266
} else {
267+
/// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
228268
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+
)*) => ($(
234275
#[inline]
235276
$(#[$attr])*
236-
pub unsafe extern fn $i($($arg: $argty),*
237-
) -> $ret {
277+
pub unsafe extern fn $i($($arg: $argty),*) -> $ret {
238278
$($body);*
239279
}
240280
)*)
241281
}
242282

283+
/// Define a safe function that is const as long as `const-extern-fn` is enabled.
243284
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+
)*) => ($(
249291
#[inline]
250292
$(#[$attr])*
251-
pub extern fn $i($($arg: $argty),*
252-
) -> $ret {
293+
pub extern fn $i($($arg: $argty),*) -> $ret {
253294
$($body);*
254295
}
255296
)*)
256297
}
257298

299+
/// A nonpublic function that is const as long as `const-extern-fn` is enabled.
258300
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+
)*) => ($(
264307
#[inline]
265308
$(#[$attr])*
266-
fn $i($($arg: $argty),*
267-
) -> $ret {
309+
fn $i($($arg: $argty),*) -> $ret {
268310
$($body);*
269311
}
270312
)*)

0 commit comments

Comments
 (0)