@@ -680,14 +680,16 @@ impl CodeGenerator for Var {
680
680
let var_ty = self . ty ( ) ;
681
681
let ty = var_ty. to_rust_ty_or_opaque ( ctx, & ( ) ) ;
682
682
683
+ let var_ty_item = ctx. resolve_item ( var_ty) ;
684
+ let alias_style = var_ty_item. alias_style ( ctx) ;
685
+ let is_new_type_alias = matches ! (
686
+ alias_style,
687
+ AliasVariation :: NewType | AliasVariation :: NewTypeDeref
688
+ ) ;
689
+
683
690
if let Some ( val) = self . val ( ) {
684
- match * val {
685
- VarType :: Bool ( val) => {
686
- result. push ( quote ! {
687
- #( #attrs) *
688
- pub const #canonical_ident : #ty = #val ;
689
- } ) ;
690
- }
691
+ let const_expr = match * val {
692
+ VarType :: Bool ( val) => Some ( val. to_token_stream ( ) ) ,
691
693
VarType :: Int ( val) => {
692
694
let int_kind = var_ty
693
695
. into_resolver ( )
@@ -702,10 +704,7 @@ impl CodeGenerator for Var {
702
704
} else {
703
705
helpers:: ast_ty:: uint_expr ( val as _ )
704
706
} ;
705
- result. push ( quote ! {
706
- #( #attrs) *
707
- pub const #canonical_ident : #ty = #val ;
708
- } ) ;
707
+ Some ( val)
709
708
}
710
709
VarType :: String ( ref bytes) => {
711
710
let prefix = ctx. trait_prefix ( ) ;
@@ -758,21 +757,20 @@ impl CodeGenerator for Var {
758
757
pub const #canonical_ident: & #( #lifetime ) * #array_ty = #bytes ;
759
758
} ) ;
760
759
}
760
+ None
761
761
}
762
- VarType :: Float ( f) => {
763
- if let Ok ( expr) = helpers:: ast_ty:: float_expr ( f) {
764
- result. push ( quote ! {
765
- #( #attrs) *
766
- pub const #canonical_ident : #ty = #expr ;
767
- } ) ;
768
- }
769
- }
770
- VarType :: Char ( c) => {
771
- result. push ( quote ! {
772
- #( #attrs) *
773
- pub const #canonical_ident : #ty = #c ;
774
- } ) ;
762
+ VarType :: Float ( f) => helpers:: ast_ty:: float_expr ( f) . ok ( ) ,
763
+ VarType :: Char ( c) => Some ( c. to_token_stream ( ) ) ,
764
+ } ;
765
+
766
+ if let Some ( mut val) = const_expr {
767
+ if is_new_type_alias {
768
+ val = quote ! { #ty( #val) } ;
775
769
}
770
+ result. push ( quote ! {
771
+ #( #attrs) *
772
+ pub const #canonical_ident : #ty = #val ;
773
+ } ) ;
776
774
}
777
775
} else {
778
776
let symbol: & str = self . link_name ( ) . unwrap_or_else ( || {
@@ -1005,15 +1003,7 @@ impl CodeGenerator for Type {
1005
1003
quote ! { }
1006
1004
} ;
1007
1005
1008
- let alias_style = if ctx. options ( ) . type_alias . matches ( & name) {
1009
- AliasVariation :: TypeAlias
1010
- } else if ctx. options ( ) . new_type_alias . matches ( & name) {
1011
- AliasVariation :: NewType
1012
- } else if ctx. options ( ) . new_type_alias_deref . matches ( & name) {
1013
- AliasVariation :: NewTypeDeref
1014
- } else {
1015
- ctx. options ( ) . default_alias_style
1016
- } ;
1006
+ let alias_style = item. alias_style ( ctx) ;
1017
1007
1018
1008
// We prefer using `pub use` over `pub type` because of:
1019
1009
// https://github.com/rust-lang/rust/issues/26264
0 commit comments