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