@@ -2170,7 +2170,10 @@ impl MethodCodegen for Method {
2170
2170
#[ derive( Copy , Clone , PartialEq , Debug ) ]
2171
2171
pub enum EnumVariation {
2172
2172
/// The code for this enum will use a Rust enum
2173
- Rust ,
2173
+ Rust {
2174
+ /// Indicates whether the generated struct should be #[non_exhaustive]
2175
+ non_exhaustive : bool
2176
+ } ,
2174
2177
/// The code for this enum will use a bitfield
2175
2178
Bitfield ,
2176
2179
/// The code for this enum will use consts
@@ -2182,14 +2185,7 @@ pub enum EnumVariation {
2182
2185
impl EnumVariation {
2183
2186
fn is_rust ( & self ) -> bool {
2184
2187
match * self {
2185
- EnumVariation :: Rust => true ,
2186
- _ => false
2187
- }
2188
- }
2189
-
2190
- fn is_bitfield ( & self ) -> bool {
2191
- match * self {
2192
- EnumVariation :: Bitfield { ..} => true ,
2188
+ EnumVariation :: Rust { .. } => true ,
2193
2189
_ => false
2194
2190
}
2195
2191
}
@@ -2216,13 +2212,14 @@ impl std::str::FromStr for EnumVariation {
2216
2212
/// Create a `EnumVariation` from a string.
2217
2213
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
2218
2214
match s {
2219
- "rust" => Ok ( EnumVariation :: Rust ) ,
2215
+ "rust" => Ok ( EnumVariation :: Rust { non_exhaustive : false } ) ,
2216
+ "rust_non_exhaustive" => Ok ( EnumVariation :: Rust { non_exhaustive : true } ) ,
2220
2217
"bitfield" => Ok ( EnumVariation :: Bitfield ) ,
2221
2218
"consts" => Ok ( EnumVariation :: Consts ) ,
2222
2219
"moduleconsts" => Ok ( EnumVariation :: ModuleConsts ) ,
2223
2220
_ => Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput ,
2224
2221
concat ! ( "Got an invalid EnumVariation. Accepted values " ,
2225
- "are 'rust', 'bitfield', 'consts', and " ,
2222
+ "are 'rust', 'rust_non_exhaustive', ' bitfield', 'consts', and " ,
2226
2223
"'moduleconsts'." ) ) ) ,
2227
2224
}
2228
2225
}
@@ -2288,7 +2285,7 @@ impl<'a> EnumBuilder<'a> {
2288
2285
}
2289
2286
}
2290
2287
2291
- EnumVariation :: Rust => {
2288
+ EnumVariation :: Rust { .. } => {
2292
2289
let tokens = quote ! ( ) ;
2293
2290
EnumBuilder :: Rust {
2294
2291
codegen_depth : enum_codegen_depth + 1 ,
@@ -2580,15 +2577,24 @@ impl CodeGenerator for Enum {
2580
2577
let variation = self . computed_enum_variation ( ctx, item) ;
2581
2578
2582
2579
// TODO(emilio): Delegate this to the builders?
2583
- if variation. is_rust ( ) {
2584
- attrs. push ( attributes:: repr ( repr_name) ) ;
2585
- } else if variation. is_bitfield ( ) {
2586
- if ctx. options ( ) . rust_features . repr_transparent {
2587
- attrs. push ( attributes:: repr ( "transparent" ) ) ;
2588
- } else {
2589
- attrs. push ( attributes:: repr ( "C" ) ) ;
2590
- }
2591
- }
2580
+ match variation {
2581
+ EnumVariation :: Rust { non_exhaustive } => {
2582
+ attrs. push ( attributes:: repr ( repr_name) ) ;
2583
+ if non_exhaustive && ctx. options ( ) . rust_features ( ) . non_exhaustive {
2584
+ attrs. push ( attributes:: non_exhaustive ( ) ) ;
2585
+ } else if non_exhaustive && !ctx. options ( ) . rust_features ( ) . non_exhaustive {
2586
+ panic ! ( "The rust target you're using doesn't seem to support non_exhaustive enums" ) ;
2587
+ }
2588
+ } ,
2589
+ EnumVariation :: Bitfield => {
2590
+ if ctx. options ( ) . rust_features . repr_transparent {
2591
+ attrs. push ( attributes:: repr ( "transparent" ) ) ;
2592
+ } else {
2593
+ attrs. push ( attributes:: repr ( "C" ) ) ;
2594
+ }
2595
+ } ,
2596
+ _ => { } ,
2597
+ } ;
2592
2598
2593
2599
if let Some ( comment) = item. comment ( ctx) {
2594
2600
attrs. push ( attributes:: doc ( comment) ) ;
0 commit comments