@@ -101,20 +101,38 @@ macro_rules! prelude {
101
101
/// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and
102
102
/// `PartialEq` if the `extra_traits` feature is enabled.
103
103
///
104
+ /// By default, it will mark a struct as `non_exhaustive`. To opt out, use the attribute
105
+ /// `#[@not_non_exhaustive]` as the first attribute of the struct.
106
+ ///
104
107
/// Use [`s_no_extra_traits`] for structs where the `extra_traits` feature does not
105
108
/// make sense, and for unions.
106
109
macro_rules! s {
107
110
( $(
111
+ $( #[ @$not_non_exhaustive: ident] ) *
108
112
$( #[ $attr: meta] ) *
109
113
pub $t: ident $i: ident { $( $field: tt) * }
110
114
) * ) => ( $(
111
- s!( it: $( #[ $attr] ) * pub $t $i { $( $field) * } ) ;
115
+ s!( it: $( #[ @$not_non_exhaustive ] ) * $ ( # [ $attr] ) * pub $t $i { $( $field) * } ) ;
112
116
) * ) ;
113
117
114
118
( it: $( #[ $attr: meta] ) * pub union $i: ident { $( $field: tt) * } ) => (
115
119
compile_error!( "unions cannot derive extra traits, use s_no_extra_traits instead" ) ;
116
120
) ;
117
121
122
+ ( it: #[ @not_non_exhaustive] $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
123
+ __item! {
124
+ #[ repr( C ) ]
125
+ #[ cfg_attr(
126
+ feature = "extra_traits" ,
127
+ :: core:: prelude:: v1:: derive( Debug , Eq , Hash , PartialEq )
128
+ ) ]
129
+ #[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
130
+ #[ allow( deprecated) ]
131
+ $( #[ $attr] ) *
132
+ pub struct $i { $( $field) * }
133
+ }
134
+ ) ;
135
+
118
136
( it: $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
119
137
__item! {
120
138
#[ repr( C ) ]
@@ -124,6 +142,7 @@ macro_rules! s {
124
142
) ]
125
143
#[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
126
144
#[ allow( deprecated) ]
145
+ #[ non_exhaustive]
127
146
$( #[ $attr] ) *
128
147
pub struct $i { $( $field) * }
129
148
}
@@ -133,6 +152,9 @@ macro_rules! s {
133
152
/// Implement `Clone` and `Copy` for a tuple struct, as well as `Debug`, `Eq`, `Hash`,
134
153
/// and `PartialEq` if the `extra_traits` feature is enabled.
135
154
///
155
+ /// By default, it will mark a struct as `non_exhaustive`. To opt out, use the attribute
156
+ /// `#[@not_non_exhaustive]` as the first attribute of the struct.
157
+ ///
136
158
/// This is the same as [`s`] but works for tuple structs.
137
159
macro_rules! s_paren {
138
160
( $(
@@ -157,10 +179,11 @@ macro_rules! s_paren {
157
179
/// Most items will prefer to use [`s`].
158
180
macro_rules! s_no_extra_traits {
159
181
( $(
182
+ $( #[ @$not_non_exhaustive: ident] ) *
160
183
$( #[ $attr: meta] ) *
161
184
pub $t: ident $i: ident { $( $field: tt) * }
162
185
) * ) => ( $(
163
- s_no_extra_traits!( it: $( #[ $attr] ) * pub $t $i { $( $field) * } ) ;
186
+ s_no_extra_traits!( it: $( #[ @$not_non_exhaustive ] ) * $ ( # [ $attr] ) * pub $t $i { $( $field) * } ) ;
164
187
) * ) ;
165
188
166
189
( it: $( #[ $attr: meta] ) * pub union $i: ident { $( $field: tt) * } ) => (
@@ -179,11 +202,22 @@ macro_rules! s_no_extra_traits {
179
202
}
180
203
) ;
181
204
205
+ ( it: #[ @not_non_exhaustive] $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
206
+ __item! {
207
+ #[ repr( C ) ]
208
+ #[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
209
+ #[ cfg_attr( feature = "extra_traits" , :: core:: prelude:: v1:: derive( Debug ) ) ]
210
+ $( #[ $attr] ) *
211
+ pub struct $i { $( $field) * }
212
+ }
213
+ ) ;
214
+
182
215
( it: $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
183
216
__item! {
184
217
#[ repr( C ) ]
185
218
#[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
186
219
#[ cfg_attr( feature = "extra_traits" , :: core:: prelude:: v1:: derive( Debug ) ) ]
220
+ #[ non_exhaustive]
187
221
$( #[ $attr] ) *
188
222
pub struct $i { $( $field) * }
189
223
}
0 commit comments