@@ -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,8 +3342,8 @@ impl EnumBuilder {
33263342 is_anonymous : enum_is_anonymous,
33273343 } ,
33283344
3329- EnumVariation :: Rust { non_exhaustive } => {
3330- EnumBuilderKind :: Rust { non_exhaustive }
3345+ EnumVariation :: Rust { non_exhaustive, repr_c } => {
3346+ EnumBuilderKind :: Rust { non_exhaustive, repr_c }
33313347 }
33323348
33333349 EnumVariation :: Consts => EnumBuilderKind :: Consts {
@@ -3539,14 +3555,18 @@ impl EnumBuilder {
35393555
35403556 // 2. Generate the enum representation
35413557 match self . kind {
3542- EnumBuilderKind :: Rust { non_exhaustive } => {
3558+ EnumBuilderKind :: Rust { non_exhaustive, repr_c } => {
35433559 let non_exhaustive_opt =
35443560 non_exhaustive. then ( attributes:: non_exhaustive) ;
35453561
3562+ let repr = repr_c
3563+ . then ( attributes:: repr_c)
3564+ . or ( Some ( quote ! { #[ repr( #enum_repr) ] } ) ) ;
3565+
35463566 quote ! {
35473567 // Note: repr is on top of attrs to keep the test expectations diff small.
35483568 // a future commit could move it further down.
3549- #[ repr( #enum_repr ) ]
3569+ #repr
35503570 #non_exhaustive_opt
35513571 #( #attrs ) *
35523572 pub enum #enum_ident {
0 commit comments