@@ -299,16 +299,11 @@ impl AppendImplicitTemplateParams for quote::Tokens {
299
299
_ => { } ,
300
300
}
301
301
302
- if let Some ( params) = item. used_template_params ( ctx) {
303
- if params. is_empty ( ) {
304
- return ;
305
- }
306
-
307
- let params = params. into_iter ( ) . map ( |p| {
308
- p. try_to_rust_ty ( ctx, & ( ) )
309
- . expect ( "template params cannot fail to be a rust type" )
310
- } ) ;
311
-
302
+ let params: Vec < _ > = item. used_template_params ( ctx) . iter ( ) . map ( |p| {
303
+ p. try_to_rust_ty ( ctx, & ( ) )
304
+ . expect ( "template params cannot fail to be a rust type" )
305
+ } ) . collect ( ) ;
306
+ if !params. is_empty ( ) {
312
307
self . append_all ( quote ! {
313
308
< #( #params ) , * >
314
309
} ) ;
@@ -633,15 +628,10 @@ impl CodeGenerator for Type {
633
628
return ;
634
629
}
635
630
636
- let mut outer_params = item. used_template_params ( ctx)
637
- . and_then ( |ps| if ps. is_empty ( ) {
638
- None
639
- } else {
640
- Some ( ps)
641
- } ) ;
631
+ let mut outer_params = item. used_template_params ( ctx) ;
642
632
643
633
let inner_rust_type = if item. is_opaque ( ctx, & ( ) ) {
644
- outer_params = None ;
634
+ outer_params = vec ! [ ] ;
645
635
self . to_opaque ( ctx, item)
646
636
} else {
647
637
// Its possible that we have better layout information than
@@ -696,7 +686,7 @@ impl CodeGenerator for Type {
696
686
'A' ...'Z' | 'a' ...'z' | '0' ...'9' | ':' | '_' | ' ' => true ,
697
687
_ => false ,
698
688
} ) &&
699
- outer_params. is_none ( ) &&
689
+ outer_params. is_empty ( ) &&
700
690
inner_item. expect_type ( ) . canonical_type ( ctx) . is_enum ( )
701
691
{
702
692
tokens. append_all ( quote ! {
@@ -715,25 +705,23 @@ impl CodeGenerator for Type {
715
705
pub type #rust_name
716
706
} ) ;
717
707
718
- if let Some ( params) = outer_params {
719
- let params: Vec < _ > = params. into_iter ( )
720
- . filter_map ( |p| p. as_template_param ( ctx, & ( ) ) )
721
- . collect ( ) ;
722
- if params. iter ( ) . any ( |p| ctx. resolve_type ( * p) . is_invalid_type_param ( ) ) {
723
- warn ! (
724
- "Item contained invalid template \
725
- parameter: {:?}",
726
- item
727
- ) ;
728
- return ;
729
- }
730
-
731
- let params = params. iter ( )
732
- . map ( |p| {
733
- p. try_to_rust_ty ( ctx, & ( ) )
734
- . expect ( "type parameters can always convert to rust ty OK" )
735
- } ) ;
708
+ let params: Vec < _ > = outer_params. into_iter ( )
709
+ . filter_map ( |p| p. as_template_param ( ctx, & ( ) ) )
710
+ . collect ( ) ;
711
+ if params. iter ( ) . any ( |p| ctx. resolve_type ( * p) . is_invalid_type_param ( ) ) {
712
+ warn ! (
713
+ "Item contained invalid template \
714
+ parameter: {:?}",
715
+ item
716
+ ) ;
717
+ return ;
718
+ }
719
+ let params: Vec < _ > = params. iter ( ) . map ( |p| {
720
+ p. try_to_rust_ty ( ctx, & ( ) )
721
+ . expect ( "type parameters can always convert to rust ty OK" )
722
+ } ) . collect ( ) ;
736
723
724
+ if !params. is_empty ( ) {
737
725
tokens. append_all ( quote ! {
738
726
< #( #params ) , * >
739
727
} ) ;
@@ -1415,8 +1403,6 @@ impl CodeGenerator for CompInfo {
1415
1403
return ;
1416
1404
}
1417
1405
1418
- let used_template_params = item. used_template_params ( ctx) ;
1419
-
1420
1406
let ty = item. expect_type ( ) ;
1421
1407
let layout = ty. layout ( ctx) ;
1422
1408
let mut packed = self . is_packed ( ctx, & layout) ;
@@ -1598,21 +1584,19 @@ impl CodeGenerator for CompInfo {
1598
1584
1599
1585
let mut generic_param_names = vec ! [ ] ;
1600
1586
1601
- if let Some ( ref params) = used_template_params {
1602
- for ( idx, ty) in params. iter ( ) . enumerate ( ) {
1603
- let param = ctx. resolve_type ( * ty) ;
1604
- let name = param. name ( ) . unwrap ( ) ;
1605
- let ident = ctx. rust_ident ( name) ;
1606
- generic_param_names. push ( ident. clone ( ) ) ;
1587
+ for ( idx, ty) in item. used_template_params ( ctx) . iter ( ) . enumerate ( ) {
1588
+ let param = ctx. resolve_type ( * ty) ;
1589
+ let name = param. name ( ) . unwrap ( ) ;
1590
+ let ident = ctx. rust_ident ( name) ;
1591
+ generic_param_names. push ( ident. clone ( ) ) ;
1607
1592
1608
- let prefix = ctx. trait_prefix ( ) ;
1609
- let field_name = ctx. rust_ident ( format ! ( "_phantom_{}" , idx) ) ;
1610
- fields. push ( quote ! {
1611
- pub #field_name : :: #prefix:: marker:: PhantomData <
1612
- :: #prefix:: cell:: UnsafeCell <#ident>
1613
- > ,
1614
- } ) ;
1615
- }
1593
+ let prefix = ctx. trait_prefix ( ) ;
1594
+ let field_name = ctx. rust_ident ( format ! ( "_phantom_{}" , idx) ) ;
1595
+ fields. push ( quote ! {
1596
+ pub #field_name : :: #prefix:: marker:: PhantomData <
1597
+ :: #prefix:: cell:: UnsafeCell <#ident>
1598
+ > ,
1599
+ } ) ;
1616
1600
}
1617
1601
1618
1602
let generics = if !generic_param_names. is_empty ( ) {
@@ -1665,11 +1649,13 @@ impl CodeGenerator for CompInfo {
1665
1649
ctx. options ( ) . derive_default && !self . is_forward_declaration ( ) ;
1666
1650
}
1667
1651
1652
+ let all_template_params = item. all_template_params ( ctx) ;
1653
+
1668
1654
if item. can_derive_copy ( ctx) && !item. annotations ( ) . disallow_copy ( ) {
1669
1655
derives. push ( "Copy" ) ;
1670
1656
1671
1657
if ctx. options ( ) . rust_features ( ) . builtin_clone_impls ||
1672
- used_template_params . is_some ( )
1658
+ !all_template_params . is_empty ( )
1673
1659
{
1674
1660
// FIXME: This requires extra logic if you have a big array in a
1675
1661
// templated struct. The reason for this is that the magic:
@@ -1751,7 +1737,7 @@ impl CodeGenerator for CompInfo {
1751
1737
) ;
1752
1738
}
1753
1739
1754
- if used_template_params . is_none ( ) {
1740
+ if all_template_params . is_empty ( ) {
1755
1741
if !is_opaque {
1756
1742
for var in self . inner_vars ( ) {
1757
1743
ctx. resolve_item ( * var) . codegen ( ctx, result, & ( ) ) ;
@@ -3020,7 +3006,6 @@ impl TryToRustTy for Type {
3020
3006
TypeKind :: TemplateAlias ( ..) |
3021
3007
TypeKind :: Alias ( ..) => {
3022
3008
let template_params = item. used_template_params ( ctx)
3023
- . unwrap_or ( vec ! [ ] )
3024
3009
. into_iter ( )
3025
3010
. filter ( |param| param. is_template_param ( ctx, & ( ) ) )
3026
3011
. collect :: < Vec < _ > > ( ) ;
@@ -3039,9 +3024,9 @@ impl TryToRustTy for Type {
3039
3024
}
3040
3025
}
3041
3026
TypeKind :: Comp ( ref info) => {
3042
- let template_params = item. used_template_params ( ctx) ;
3027
+ let template_params = item. all_template_params ( ctx) ;
3043
3028
if info. has_non_type_template_params ( ) ||
3044
- ( item. is_opaque ( ctx, & ( ) ) && template_params. is_some ( ) )
3029
+ ( item. is_opaque ( ctx, & ( ) ) && ! template_params. is_empty ( ) )
3045
3030
{
3046
3031
return self . try_to_opaque ( ctx, item) ;
3047
3032
}
0 commit comments