@@ -3168,6 +3168,8 @@ pub enum EnumVariation {
3168
3168
Rust {
3169
3169
/// Indicates whether the generated struct should be `#[non_exhaustive]`
3170
3170
non_exhaustive : bool ,
3171
+ /// Indicates whether the generated struct should be `#[repr(C)]`
3172
+ repr_c : bool ,
3171
3173
} ,
3172
3174
/// The code for this enum will use a newtype
3173
3175
NewType {
@@ -3199,11 +3201,14 @@ impl fmt::Display for EnumVariation {
3199
3201
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3200
3202
let s = match self {
3201
3203
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
+ } ,
3207
3212
Self :: NewType {
3208
3213
is_bitfield : true , ..
3209
3214
} => "bitfield" ,
@@ -3232,9 +3237,19 @@ impl FromStr for EnumVariation {
3232
3237
match s {
3233
3238
"rust" => Ok ( EnumVariation :: Rust {
3234
3239
non_exhaustive : false ,
3240
+ repr_c : false ,
3241
+ } ) ,
3242
+ "rust_repr_c" => Ok ( EnumVariation :: Rust {
3243
+ non_exhaustive : false ,
3244
+ repr_c : true ,
3235
3245
} ) ,
3236
3246
"rust_non_exhaustive" => Ok ( EnumVariation :: Rust {
3237
3247
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 ,
3238
3253
} ) ,
3239
3254
"bitfield" => Ok ( EnumVariation :: NewType {
3240
3255
is_bitfield : true ,
@@ -3281,6 +3296,7 @@ struct EnumBuilder {
3281
3296
enum EnumBuilderKind {
3282
3297
Rust {
3283
3298
non_exhaustive : bool ,
3299
+ repr_c : bool ,
3284
3300
} ,
3285
3301
NewType {
3286
3302
is_bitfield : bool ,
@@ -3326,8 +3342,8 @@ impl EnumBuilder {
3326
3342
is_anonymous : enum_is_anonymous,
3327
3343
} ,
3328
3344
3329
- EnumVariation :: Rust { non_exhaustive } => {
3330
- EnumBuilderKind :: Rust { non_exhaustive }
3345
+ EnumVariation :: Rust { non_exhaustive, repr_c } => {
3346
+ EnumBuilderKind :: Rust { non_exhaustive, repr_c }
3331
3347
}
3332
3348
3333
3349
EnumVariation :: Consts => EnumBuilderKind :: Consts {
@@ -3539,14 +3555,18 @@ impl EnumBuilder {
3539
3555
3540
3556
// 2. Generate the enum representation
3541
3557
match self . kind {
3542
- EnumBuilderKind :: Rust { non_exhaustive } => {
3558
+ EnumBuilderKind :: Rust { non_exhaustive, repr_c } => {
3543
3559
let non_exhaustive_opt =
3544
3560
non_exhaustive. then ( attributes:: non_exhaustive) ;
3545
3561
3562
+ let repr = repr_c
3563
+ . then ( attributes:: repr_c)
3564
+ . or ( Some ( quote ! { #[ repr( #enum_repr) ] } ) ) ;
3565
+
3546
3566
quote ! {
3547
3567
// Note: repr is on top of attrs to keep the test expectations diff small.
3548
3568
// a future commit could move it further down.
3549
- #[ repr( #enum_repr ) ]
3569
+ #repr
3550
3570
#non_exhaustive_opt
3551
3571
#( #attrs ) *
3552
3572
pub enum #enum_ident {
0 commit comments