Skip to content

Commit c5fe2e3

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. (backport <#4783>) (cherry picked from commit cdcc466)
1 parent 0ebb204 commit c5fe2e3

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
}
@@ -529,3 +529,41 @@ mod tests {
529529
TypeId::of::<T>()
530530
}
531531
}
532+
533+
#[cfg(test)]
534+
#[allow(unused)]
535+
mod macro_checks {
536+
s! {
537+
pub struct S1 {
538+
pub a: u32,
539+
b: u32,
540+
}
541+
542+
struct S1Priv {
543+
pub a: u32,
544+
b: u32,
545+
}
546+
}
547+
548+
s_no_extra_traits! {
549+
pub struct S2 {
550+
pub a: u32,
551+
b: u32,
552+
}
553+
554+
struct S2Priv {
555+
pub a: u32,
556+
b: u32,
557+
}
558+
559+
pub union U2 {
560+
pub a: u32,
561+
b: f32,
562+
}
563+
564+
union U2Priv {
565+
pub a: u32,
566+
b: f32,
567+
}
568+
}
569+
}

0 commit comments

Comments
 (0)