Skip to content

Commit cdcc466

Browse files
committed
macros: Allow s! and s_no_extra_traits! to take private items
Occasionally we need to define structs that keep the expected set of traits but aren't crate-public. Add support to the macros to do this.
1 parent a53f4bc commit cdcc466

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

src/macros.rs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,16 @@ macro_rules! prelude {
142142
macro_rules! s {
143143
($(
144144
$(#[$attr:meta])*
145-
pub $t:ident $i:ident { $($field:tt)* }
145+
$pub:vis $t:ident $i:ident { $($field:tt)* }
146146
)*) => ($(
147-
s!(it: $(#[$attr])* pub $t $i { $($field)* });
147+
s!(it: $(#[$attr])* $pub $t $i { $($field)* });
148148
)*);
149149

150-
(it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
150+
(it: $(#[$attr:meta])* $pub:vis union $i:ident { $($field:tt)* }) => (
151151
compile_error!("unions cannot derive extra traits, use s_no_extra_traits instead");
152152
);
153153

154-
(it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
154+
(it: $(#[$attr:meta])* $pub:vis struct $i:ident { $($field:tt)* }) => (
155155
__item! {
156156
#[repr(C)]
157157
#[cfg_attr(
@@ -165,7 +165,7 @@ macro_rules! s {
165165
)]
166166
#[allow(deprecated)]
167167
$(#[$attr])*
168-
pub struct $i { $($field)* }
168+
$pub struct $i { $($field)* }
169169
}
170170
);
171171
}
@@ -202,17 +202,17 @@ macro_rules! s_paren {
202202
macro_rules! s_no_extra_traits {
203203
($(
204204
$(#[$attr:meta])*
205-
pub $t:ident $i:ident { $($field:tt)* }
205+
$pub:vis $t:ident $i:ident { $($field:tt)* }
206206
)*) => ($(
207-
s_no_extra_traits!(it: $(#[$attr])* pub $t $i { $($field)* });
207+
s_no_extra_traits!(it: $(#[$attr])* $pub $t $i { $($field)* });
208208
)*);
209209

210-
(it: $(#[$attr:meta])* pub union $i:ident { $($field:tt)* }) => (
210+
(it: $(#[$attr:meta])* $pub:vis union $i:ident { $($field:tt)* }) => (
211211
__item! {
212212
#[repr(C)]
213213
#[::core::prelude::v1::derive(::core::clone::Clone, ::core::marker::Copy)]
214214
$(#[$attr])*
215-
pub union $i { $($field)* }
215+
$pub union $i { $($field)* }
216216
}
217217

218218
impl ::core::fmt::Debug for $i {
@@ -222,7 +222,7 @@ macro_rules! s_no_extra_traits {
222222
}
223223
);
224224

225-
(it: $(#[$attr:meta])* pub struct $i:ident { $($field:tt)* }) => (
225+
(it: $(#[$attr:meta])* $pub:vis struct $i:ident { $($field:tt)* }) => (
226226
__item! {
227227
#[repr(C)]
228228
#[::core::prelude::v1::derive(
@@ -231,7 +231,7 @@ macro_rules! s_no_extra_traits {
231231
::core::fmt::Debug,
232232
)]
233233
$(#[$attr])*
234-
pub struct $i { $($field)* }
234+
$pub struct $i { $($field)* }
235235
}
236236
);
237237
}
@@ -491,3 +491,41 @@ mod tests {
491491
TypeId::of::<T>()
492492
}
493493
}
494+
495+
#[cfg(test)]
496+
#[allow(unused)]
497+
mod macro_checks {
498+
s! {
499+
pub struct S1 {
500+
pub a: u32,
501+
b: u32,
502+
}
503+
504+
struct S1Priv {
505+
pub a: u32,
506+
b: u32,
507+
}
508+
}
509+
510+
s_no_extra_traits! {
511+
pub struct S2 {
512+
pub a: u32,
513+
b: u32,
514+
}
515+
516+
struct S2Priv {
517+
pub a: u32,
518+
b: u32,
519+
}
520+
521+
pub union U2 {
522+
pub a: u32,
523+
b: f32,
524+
}
525+
526+
union U2Priv {
527+
pub a: u32,
528+
b: f32,
529+
}
530+
}
531+
}

0 commit comments

Comments
 (0)