@@ -2438,6 +2438,7 @@ enum EnumBuilder<'a> {
2438
2438
is_bitfield : bool ,
2439
2439
} ,
2440
2440
Consts {
2441
+ repr : proc_macro2:: TokenStream ,
2441
2442
variants : Vec < proc_macro2:: TokenStream > ,
2442
2443
codegen_depth : usize ,
2443
2444
} ,
@@ -2459,6 +2460,14 @@ impl<'a> EnumBuilder<'a> {
2459
2460
}
2460
2461
}
2461
2462
2463
+ /// Returns true if the builder is for a rustified enum.
2464
+ fn is_rust_enum ( & self ) -> bool {
2465
+ match * self {
2466
+ EnumBuilder :: Rust { .. } => true ,
2467
+ _ => false ,
2468
+ }
2469
+ }
2470
+
2462
2471
/// Create a new enum given an item builder, a canonical name, a name for
2463
2472
/// the representation, and which variation it should be generated as.
2464
2473
fn new (
@@ -2467,6 +2476,7 @@ impl<'a> EnumBuilder<'a> {
2467
2476
repr : proc_macro2:: TokenStream ,
2468
2477
enum_variation : EnumVariation ,
2469
2478
enum_codegen_depth : usize ,
2479
+ is_ty_named : bool ,
2470
2480
) -> Self {
2471
2481
let ident = Ident :: new ( name, Span :: call_site ( ) ) ;
2472
2482
@@ -2492,13 +2502,22 @@ impl<'a> EnumBuilder<'a> {
2492
2502
}
2493
2503
}
2494
2504
2495
- EnumVariation :: Consts => EnumBuilder :: Consts {
2496
- variants : vec ! [ quote! {
2497
- #( #attrs ) *
2498
- pub type #ident = #repr;
2499
- } ] ,
2500
- codegen_depth : enum_codegen_depth,
2501
- } ,
2505
+ EnumVariation :: Consts => {
2506
+ let mut variants = Vec :: new ( ) ;
2507
+
2508
+ if is_ty_named {
2509
+ variants. push ( quote ! {
2510
+ #( #attrs ) *
2511
+ pub type #ident = #repr;
2512
+ } ) ;
2513
+ }
2514
+
2515
+ EnumBuilder :: Consts {
2516
+ repr : repr,
2517
+ variants : variants,
2518
+ codegen_depth : enum_codegen_depth,
2519
+ }
2520
+ }
2502
2521
2503
2522
EnumVariation :: ModuleConsts => {
2504
2523
let ident = Ident :: new (
@@ -2530,7 +2549,12 @@ impl<'a> EnumBuilder<'a> {
2530
2549
is_ty_named : bool ,
2531
2550
) -> Self {
2532
2551
let variant_name = ctx. rust_mangle ( variant. name ( ) ) ;
2552
+ let is_rust_enum = self . is_rust_enum ( ) ;
2533
2553
let expr = match variant. val ( ) {
2554
+ EnumVariantValue :: Boolean ( v) if is_rust_enum => {
2555
+ helpers:: ast_ty:: uint_expr ( v as u64 )
2556
+ }
2557
+ EnumVariantValue :: Boolean ( v) => quote ! ( #v) ,
2534
2558
EnumVariantValue :: Signed ( v) => helpers:: ast_ty:: int_expr ( v) ,
2535
2559
EnumVariantValue :: Unsigned ( v) => helpers:: ast_ty:: uint_expr ( v) ,
2536
2560
} ;
@@ -2593,18 +2617,20 @@ impl<'a> EnumBuilder<'a> {
2593
2617
self
2594
2618
}
2595
2619
2596
- EnumBuilder :: Consts { .. } => {
2620
+ EnumBuilder :: Consts { ref repr , .. } => {
2597
2621
let constant_name = match mangling_prefix {
2598
2622
Some ( prefix) => {
2599
2623
Cow :: Owned ( format ! ( "{}_{}" , prefix, variant_name) )
2600
2624
}
2601
2625
None => variant_name,
2602
2626
} ;
2603
2627
2628
+ let ty = if is_ty_named { & rust_ty } else { repr } ;
2629
+
2604
2630
let ident = ctx. rust_ident ( constant_name) ;
2605
2631
result. push ( quote ! {
2606
2632
#doc
2607
- pub const #ident : #rust_ty = #expr ;
2633
+ pub const #ident : #ty = #expr ;
2608
2634
} ) ;
2609
2635
2610
2636
self
@@ -2859,9 +2885,12 @@ impl CodeGenerator for Enum {
2859
2885
} ) ;
2860
2886
}
2861
2887
2862
- let repr = {
2863
- let repr_name = ctx. rust_ident_raw ( repr_name) ;
2864
- quote ! { #repr_name }
2888
+ let repr = match self . repr ( ) {
2889
+ Some ( ty) => ty. to_rust_ty_or_opaque ( ctx, & ( ) ) ,
2890
+ None => {
2891
+ let repr_name = ctx. rust_ident_raw ( repr_name) ;
2892
+ quote ! { #repr_name }
2893
+ }
2865
2894
} ;
2866
2895
2867
2896
let mut builder = EnumBuilder :: new (
@@ -2870,6 +2899,7 @@ impl CodeGenerator for Enum {
2870
2899
repr,
2871
2900
variation,
2872
2901
item. codegen_depth ( ctx) ,
2902
+ enum_ty. name ( ) . is_some ( ) ,
2873
2903
) ;
2874
2904
2875
2905
// A map where we keep a value -> variant relation.
0 commit comments