@@ -2170,7 +2170,10 @@ impl MethodCodegen for Method {
21702170#[ derive( Copy , Clone , PartialEq , Debug ) ]
21712171pub enum EnumVariation {
21722172 /// The code for this enum will use a Rust enum
2173- Rust ,
2173+ ///
2174+ /// When the boolean parameter on this variant is set to true,
2175+ /// the generated enum should be non_exhaustive.
2176+ Rust ( bool ) ,
21742177 /// The code for this enum will use a bitfield
21752178 Bitfield ,
21762179 /// The code for this enum will use consts
@@ -2182,14 +2185,7 @@ pub enum EnumVariation {
21822185impl EnumVariation {
21832186 fn is_rust ( & self ) -> bool {
21842187 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 ,
21932189 _ => false
21942190 }
21952191 }
@@ -2216,13 +2212,14 @@ impl std::str::FromStr for EnumVariation {
22162212 /// Create a `EnumVariation` from a string.
22172213 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
22182214 match s {
2219- "rust" => Ok ( EnumVariation :: Rust ) ,
2215+ "rust" => Ok ( EnumVariation :: Rust ( false ) ) ,
2216+ "rust_non_exhaustive" => Ok ( EnumVariation :: Rust ( true ) ) ,
22202217 "bitfield" => Ok ( EnumVariation :: Bitfield ) ,
22212218 "consts" => Ok ( EnumVariation :: Consts ) ,
22222219 "moduleconsts" => Ok ( EnumVariation :: ModuleConsts ) ,
22232220 _ => Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput ,
22242221 concat ! ( "Got an invalid EnumVariation. Accepted values " ,
2225- "are 'rust', 'bitfield', 'consts', and " ,
2222+ "are 'rust', 'rust_non_exhaustive', ' bitfield', 'consts', and " ,
22262223 "'moduleconsts'." ) ) ) ,
22272224 }
22282225 }
@@ -2288,7 +2285,7 @@ impl<'a> EnumBuilder<'a> {
22882285 }
22892286 }
22902287
2291- EnumVariation :: Rust => {
2288+ EnumVariation :: Rust ( _ ) => {
22922289 let tokens = quote ! ( ) ;
22932290 EnumBuilder :: Rust {
22942291 codegen_depth : enum_codegen_depth + 1 ,
@@ -2580,15 +2577,22 @@ impl CodeGenerator for Enum {
25802577 let variation = self . computed_enum_variation ( ctx, item) ;
25812578
25822579 // 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 {
2584+ attrs. push ( attributes:: non_exhaustive ( ) ) ;
2585+ }
2586+ } ,
2587+ EnumVariation :: Bitfield => {
2588+ if ctx. options ( ) . rust_features . repr_transparent {
2589+ attrs. push ( attributes:: repr ( "transparent" ) ) ;
2590+ } else {
2591+ attrs. push ( attributes:: repr ( "C" ) ) ;
2592+ }
2593+ } ,
2594+ _ => { } ,
2595+ } ;
25922596
25932597 if let Some ( comment) = item. comment ( ctx) {
25942598 attrs. push ( attributes:: doc ( comment) ) ;
0 commit comments