@@ -93,20 +93,38 @@ macro_rules! prelude {
93
93
/// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and
94
94
/// `PartialEq` if the `extra_traits` feature is enabled.
95
95
///
96
+ /// By default, it will mark a struct as `non_exhaustive`. To opt out, use the attribute
97
+ /// `#[@not_non_exhaustive]` as the first attribute of the struct.
98
+ ///
96
99
/// Use [`s_no_extra_traits`] for structs where the `extra_traits` feature does not
97
100
/// make sense, and for unions.
98
101
macro_rules! s {
99
102
( $(
103
+ $( #[ @$not_non_exhaustive: ident] ) ?
100
104
$( #[ $attr: meta] ) *
101
105
pub $t: ident $i: ident { $( $field: tt) * }
102
106
) * ) => ( $(
103
- s!( it: $( #[ $attr] ) * pub $t $i { $( $field) * } ) ;
107
+ s!( it: $( #[ @$not_non_exhaustive ] ) ? $ ( # [ $attr] ) * pub $t $i { $( $field) * } ) ;
104
108
) * ) ;
105
109
106
110
( it: $( #[ $attr: meta] ) * pub union $i: ident { $( $field: tt) * } ) => (
107
111
compile_error!( "unions cannot derive extra traits, use s_no_extra_traits instead" ) ;
108
112
) ;
109
113
114
+ ( it: #[ @not_non_exhaustive] $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
115
+ __item! {
116
+ #[ repr( C ) ]
117
+ #[ cfg_attr(
118
+ feature = "extra_traits" ,
119
+ :: core:: prelude:: v1:: derive( Debug , Eq , Hash , PartialEq )
120
+ ) ]
121
+ #[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
122
+ #[ allow( deprecated) ]
123
+ $( #[ $attr] ) *
124
+ pub struct $i { $( $field) * }
125
+ }
126
+ ) ;
127
+
110
128
( it: $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
111
129
__item! {
112
130
#[ repr( C ) ]
@@ -116,6 +134,7 @@ macro_rules! s {
116
134
) ]
117
135
#[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
118
136
#[ allow( deprecated) ]
137
+ #[ non_exhaustive]
119
138
$( #[ $attr] ) *
120
139
pub struct $i { $( $field) * }
121
140
}
@@ -125,6 +144,9 @@ macro_rules! s {
125
144
/// Implement `Clone` and `Copy` for a tuple struct, as well as `Debug`, `Eq`, `Hash`,
126
145
/// and `PartialEq` if the `extra_traits` feature is enabled.
127
146
///
147
+ /// By default, it will mark a struct as `non_exhaustive`. To opt out, use the attribute
148
+ /// `#[@not_non_exhaustive]` as the first attribute of the struct.
149
+ ///
128
150
/// This is the same as [`s`] but works for tuple structs.
129
151
macro_rules! s_paren {
130
152
( $(
@@ -149,10 +171,11 @@ macro_rules! s_paren {
149
171
/// Most items will prefer to use [`s`].
150
172
macro_rules! s_no_extra_traits {
151
173
( $(
174
+ $( #[ @$not_non_exhaustive: ident] ) ?
152
175
$( #[ $attr: meta] ) *
153
176
pub $t: ident $i: ident { $( $field: tt) * }
154
177
) * ) => ( $(
155
- s_no_extra_traits!( it: $( #[ $attr] ) * pub $t $i { $( $field) * } ) ;
178
+ s_no_extra_traits!( it: $( #[ @$not_non_exhaustive ] ) ? $ ( # [ $attr] ) * pub $t $i { $( $field) * } ) ;
156
179
) * ) ;
157
180
158
181
( it: $( #[ $attr: meta] ) * pub union $i: ident { $( $field: tt) * } ) => (
@@ -171,11 +194,22 @@ macro_rules! s_no_extra_traits {
171
194
}
172
195
) ;
173
196
197
+ ( it: #[ @not_non_exhaustive] $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
198
+ __item! {
199
+ #[ repr( C ) ]
200
+ #[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
201
+ #[ cfg_attr( feature = "extra_traits" , :: core:: prelude:: v1:: derive( Debug ) ) ]
202
+ $( #[ $attr] ) *
203
+ pub struct $i { $( $field) * }
204
+ }
205
+ ) ;
206
+
174
207
( it: $( #[ $attr: meta] ) * pub struct $i: ident { $( $field: tt) * } ) => (
175
208
__item! {
176
209
#[ repr( C ) ]
177
210
#[ :: core:: prelude:: v1:: derive( :: core:: clone:: Clone , :: core:: marker:: Copy ) ]
178
211
#[ cfg_attr( feature = "extra_traits" , :: core:: prelude:: v1:: derive( Debug ) ) ]
212
+ #[ non_exhaustive]
179
213
$( #[ $attr] ) *
180
214
pub struct $i { $( $field) * }
181
215
}
0 commit comments