@@ -3168,6 +3168,8 @@ pub enum EnumVariation {
31683168 Rust {
31693169 /// Indicates whether the generated struct should be `#[non_exhaustive]`
31703170 non_exhaustive : bool ,
3171+ /// Indicates whether the generated struct should be `#[repr(C)]`
3172+ repr_c : bool ,
31713173 } ,
31723174 /// The code for this enum will use a newtype
31733175 NewType {
@@ -3199,11 +3201,14 @@ impl fmt::Display for EnumVariation {
31993201 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
32003202 let s = match self {
32013203 Self :: Rust {
3202- non_exhaustive : false ,
3203- } => "rust" ,
3204- Self :: Rust {
3205- non_exhaustive : true ,
3206- } => "rust_non_exhaustive" ,
3204+ non_exhaustive,
3205+ repr_c,
3206+ } => match ( * non_exhaustive, * repr_c) {
3207+ ( false , false ) => "rust" ,
3208+ ( false , true ) => "rust_repr_c" ,
3209+ ( true , false ) => "rust_non_exhaustive" ,
3210+ ( true , true ) => "rust_non_exhaustive_repr_c" ,
3211+ } ,
32073212 Self :: NewType {
32083213 is_bitfield : true , ..
32093214 } => "bitfield" ,
@@ -3232,9 +3237,19 @@ impl FromStr for EnumVariation {
32323237 match s {
32333238 "rust" => Ok ( EnumVariation :: Rust {
32343239 non_exhaustive : false ,
3240+ repr_c : false ,
3241+ } ) ,
3242+ "rust_repr_c" => Ok ( EnumVariation :: Rust {
3243+ non_exhaustive : false ,
3244+ repr_c : true ,
32353245 } ) ,
32363246 "rust_non_exhaustive" => Ok ( EnumVariation :: Rust {
32373247 non_exhaustive : true ,
3248+ repr_c : false ,
3249+ } ) ,
3250+ "rust_non_exhaustive_repr_c" => Ok ( EnumVariation :: Rust {
3251+ non_exhaustive : true ,
3252+ repr_c : true ,
32383253 } ) ,
32393254 "bitfield" => Ok ( EnumVariation :: NewType {
32403255 is_bitfield : true ,
@@ -3281,6 +3296,7 @@ struct EnumBuilder {
32813296enum EnumBuilderKind {
32823297 Rust {
32833298 non_exhaustive : bool ,
3299+ repr_c : bool ,
32843300 } ,
32853301 NewType {
32863302 is_bitfield : bool ,
@@ -3326,9 +3342,13 @@ impl EnumBuilder {
33263342 is_anonymous : enum_is_anonymous,
33273343 } ,
33283344
3329- EnumVariation :: Rust { non_exhaustive } => {
3330- EnumBuilderKind :: Rust { non_exhaustive }
3331- }
3345+ EnumVariation :: Rust {
3346+ non_exhaustive,
3347+ repr_c,
3348+ } => EnumBuilderKind :: Rust {
3349+ non_exhaustive,
3350+ repr_c,
3351+ } ,
33323352
33333353 EnumVariation :: Consts => EnumBuilderKind :: Consts {
33343354 needs_typedef : !has_typedef,
@@ -3539,14 +3559,21 @@ impl EnumBuilder {
35393559
35403560 // 2. Generate the enum representation
35413561 match self . kind {
3542- EnumBuilderKind :: Rust { non_exhaustive } => {
3562+ EnumBuilderKind :: Rust {
3563+ non_exhaustive,
3564+ repr_c,
3565+ } => {
35433566 let non_exhaustive_opt =
35443567 non_exhaustive. then ( attributes:: non_exhaustive) ;
35453568
3569+ let repr = repr_c
3570+ . then ( attributes:: repr_c)
3571+ . or ( Some ( quote ! { #[ repr( #enum_repr) ] } ) ) ;
3572+
35463573 quote ! {
35473574 // Note: repr is on top of attrs to keep the test expectations diff small.
35483575 // a future commit could move it further down.
3549- #[ repr( #enum_repr ) ]
3576+ #repr
35503577 #non_exhaustive_opt
35513578 #( #attrs ) *
35523579 pub enum #enum_ident {
0 commit comments