@@ -282,20 +282,21 @@ impl AppendImplicitTemplateParams for quote::Tokens {
282
282
_ => { } ,
283
283
}
284
284
285
- if let Some ( params) = item. used_template_params ( ctx) {
286
- if params. is_empty ( ) {
287
- return ;
288
- }
285
+ let params = item. used_template_params ( ctx) ;
289
286
290
- let params = params. into_iter ( ) . map ( |p| {
291
- p. try_to_rust_ty ( ctx, & ( ) )
292
- . expect ( "template params cannot fail to be a rust type" )
293
- } ) ;
294
-
295
- self . append ( quote ! {
296
- < #( #params ) , * >
297
- } ) ;
287
+ if params. is_empty ( ) {
288
+ return ;
298
289
}
290
+
291
+ let params = params. into_iter ( ) . map ( |p| {
292
+ p. try_to_rust_ty ( ctx, & ( ) )
293
+ . expect ( "template params cannot fail to be a rust type" )
294
+ } ) ;
295
+
296
+ self . append ( quote ! {
297
+ < #( #params ) , * >
298
+ } ) ;
299
+
299
300
}
300
301
}
301
302
@@ -459,11 +460,9 @@ impl CodeGenerator for Var {
459
460
// We can't generate bindings to static variables of templates. The
460
461
// number of actual variables for a single declaration are open ended
461
462
// and we don't know what instantiations do or don't exist.
462
- let type_params = item. all_template_params ( ctx) ;
463
- if let Some ( params) = type_params {
464
- if !params. is_empty ( ) {
465
- return ;
466
- }
463
+
464
+ if !item. all_template_params ( ctx) . is_empty ( ) {
465
+ return
467
466
}
468
467
469
468
let ty = self . ty ( ) . to_rust_ty_or_opaque ( ctx, & ( ) ) ;
@@ -605,15 +604,15 @@ impl CodeGenerator for Type {
605
604
return ;
606
605
}
607
606
608
- let mut outer_params = item . used_template_params ( ctx )
609
- . and_then ( |ps| if ps . is_empty ( ) {
610
- None
607
+ let mut outer_params =
608
+ if !item . used_template_params ( ctx ) . is_empty ( ) {
609
+ item . used_template_params ( ctx ) . clone ( )
611
610
} else {
612
- Some ( ps )
613
- } ) ;
611
+ Vec :: new ( )
612
+ } ;
614
613
615
614
let inner_rust_type = if item. is_opaque ( ctx, & ( ) ) {
616
- outer_params = None ;
615
+ outer_params = Vec :: new ( ) ;
617
616
self . to_opaque ( ctx, item)
618
617
} else {
619
618
// Its possible that we have better layout information than
@@ -668,7 +667,7 @@ impl CodeGenerator for Type {
668
667
'A' ...'Z' | 'a' ...'z' | '0' ...'9' | ':' | '_' | ' ' => true ,
669
668
_ => false ,
670
669
} ) &&
671
- outer_params. is_none ( ) &&
670
+ outer_params. is_empty ( ) &&
672
671
inner_item. expect_type ( ) . canonical_type ( ctx) . is_enum ( )
673
672
{
674
673
tokens. append ( quote ! {
@@ -687,8 +686,8 @@ impl CodeGenerator for Type {
687
686
pub type #rust_name
688
687
} ) ;
689
688
690
- if let Some ( params ) = outer_params {
691
- let params: Vec < _ > = params . into_iter ( )
689
+ if !outer_params . is_empty ( ) {
690
+ let params: Vec < _ > = outer_params . into_iter ( )
692
691
. filter_map ( |p| p. as_template_param ( ctx, & ( ) ) )
693
692
. collect ( ) ;
694
693
if params. iter ( ) . any ( |p| ctx. resolve_type ( * p) . is_invalid_type_param ( ) ) {
@@ -1405,7 +1404,7 @@ impl CodeGenerator for CompInfo {
1405
1404
//
1406
1405
// NB: We generate a proper struct to avoid struct/function name
1407
1406
// collisions.
1408
- if self . is_forward_declaration ( ) && used_template_params. is_none ( ) {
1407
+ if self . is_forward_declaration ( ) && used_template_params. is_empty ( ) {
1409
1408
let struct_name = item. canonical_name ( ctx) ;
1410
1409
let struct_name = ctx. rust_ident_raw ( struct_name) ;
1411
1410
let tuple_struct = quote ! {
@@ -1450,7 +1449,7 @@ impl CodeGenerator for CompInfo {
1450
1449
ctx. options ( ) . derive_copy
1451
1450
{
1452
1451
derives. push ( "Copy" ) ;
1453
- if used_template_params. is_some ( ) {
1452
+ if used_template_params. is_empty ( ) {
1454
1453
// FIXME: This requires extra logic if you have a big array in a
1455
1454
// templated struct. The reason for this is that the magic:
1456
1455
// fn clone(&self) -> Self { *self }
@@ -1478,8 +1477,8 @@ impl CodeGenerator for CompInfo {
1478
1477
if item. can_derive_partialeq ( ctx) {
1479
1478
derives. push ( "PartialEq" ) ;
1480
1479
} else {
1481
- needs_partialeq_impl =
1482
- ctx. options ( ) . derive_partialeq &&
1480
+ needs_partialeq_impl =
1481
+ ctx. options ( ) . derive_partialeq &&
1483
1482
ctx. options ( ) . impl_partialeq &&
1484
1483
ctx. lookup_can_derive_partialeq_or_partialord ( item. id ( ) )
1485
1484
. map_or ( true , |x| {
@@ -1668,7 +1667,9 @@ impl CodeGenerator for CompInfo {
1668
1667
1669
1668
let mut generic_param_names = vec ! [ ] ;
1670
1669
1671
- if let Some ( ref params) = used_template_params {
1670
+ let ref params = used_template_params;
1671
+
1672
+ if !params. is_empty ( ) {
1672
1673
for ( idx, ty) in params. iter ( ) . enumerate ( ) {
1673
1674
let param = ctx. resolve_type ( * ty) ;
1674
1675
let name = param. name ( ) . unwrap ( ) ;
@@ -1678,13 +1679,14 @@ impl CodeGenerator for CompInfo {
1678
1679
let prefix = ctx. trait_prefix ( ) ;
1679
1680
let field_name = ctx. rust_ident ( format ! ( "_phantom_{}" , idx) ) ;
1680
1681
fields. push ( quote ! {
1681
- pub #field_name : :: #prefix:: marker:: PhantomData <
1682
- :: #prefix:: cell:: UnsafeCell <#ident>
1683
- > ,
1684
- } ) ;
1682
+ pub #field_name : :: #prefix:: marker:: PhantomData <
1683
+ :: #prefix:: cell:: UnsafeCell <#ident>
1684
+ > ,
1685
+ } ) ;
1685
1686
}
1686
1687
}
1687
1688
1689
+
1688
1690
let generics = if !generic_param_names. is_empty ( ) {
1689
1691
let generic_param_names = generic_param_names. clone ( ) ;
1690
1692
quote ! {
@@ -1721,7 +1723,7 @@ impl CodeGenerator for CompInfo {
1721
1723
) ;
1722
1724
}
1723
1725
1724
- if used_template_params. is_none ( ) {
1726
+ if used_template_params. is_empty ( ) {
1725
1727
if !is_opaque {
1726
1728
for var in self . inner_vars ( ) {
1727
1729
ctx. resolve_item ( * var) . codegen ( ctx, result, & ( ) ) ;
@@ -1898,7 +1900,7 @@ impl CodeGenerator for CompInfo {
1898
1900
1899
1901
if needs_partialeq_impl {
1900
1902
if let Some ( impl_) = impl_partialeq:: gen_partialeq_impl ( ctx, self , item, & ty_for_impl) {
1901
-
1903
+
1902
1904
let partialeq_bounds = if !generic_param_names. is_empty ( ) {
1903
1905
let bounds = generic_param_names. iter ( ) . map ( |t| {
1904
1906
quote ! { #t: PartialEq }
@@ -2919,10 +2921,10 @@ impl TryToRustTy for Type {
2919
2921
TypeKind :: TemplateAlias ( ..) |
2920
2922
TypeKind :: Alias ( ..) => {
2921
2923
let template_params = item. used_template_params ( ctx)
2922
- . unwrap_or ( vec ! [ ] )
2923
- . into_iter ( )
2924
- . filter ( |param| param. is_template_param ( ctx, & ( ) ) )
2925
- . collect :: < Vec < _ > > ( ) ;
2924
+
2925
+ . into_iter ( )
2926
+ . filter ( |param| param. is_template_param ( ctx, & ( ) ) )
2927
+ . collect :: < Vec < _ > > ( ) ;
2926
2928
2927
2929
let spelling = self . name ( ) . expect ( "Unnamed alias?" ) ;
2928
2930
if item. is_opaque ( ctx, & ( ) ) && !template_params. is_empty ( ) {
@@ -2940,7 +2942,7 @@ impl TryToRustTy for Type {
2940
2942
TypeKind :: Comp ( ref info) => {
2941
2943
let template_params = item. used_template_params ( ctx) ;
2942
2944
if info. has_non_type_template_params ( ) ||
2943
- ( item. is_opaque ( ctx, & ( ) ) && template_params. is_some ( ) )
2945
+ ( item. is_opaque ( ctx, & ( ) ) && ! template_params. is_empty ( ) )
2944
2946
{
2945
2947
return self . try_to_opaque ( ctx, item) ;
2946
2948
}
@@ -3034,18 +3036,18 @@ impl TryToRustTy for TemplateInstantiation {
3034
3036
let def_path = def. namespace_aware_canonical_path ( ctx) ;
3035
3037
ty. append_separated ( def_path. into_iter ( ) . map ( |p| ctx. rust_ident ( p) ) , "::" ) ;
3036
3038
3037
- let def_params = match def. self_template_params ( ctx) {
3038
- Some ( params ) => params ,
3039
- None => {
3040
- // This can happen if we generated an opaque type for a partial
3041
- // template specialization, and we've hit an instantiation of
3042
- // that partial specialization.
3043
- extra_assert ! (
3044
- def. is_opaque( ctx, & ( ) )
3045
- ) ;
3046
- return Err ( error:: Error :: InstantiationOfOpaqueType ) ;
3047
- }
3048
- } ;
3039
+ let def_params = def. self_template_params ( ctx) ;
3040
+
3041
+ if def_params . is_empty ( ) {
3042
+ // This can happen if we generated an opaque type for a partial
3043
+ // template specialization, and we've hit an instantiation of
3044
+ // that partial specialization.
3045
+ extra_assert ! (
3046
+ def. is_opaque( ctx, & ( ) )
3047
+ ) ;
3048
+ return Err ( error:: Error :: InstantiationOfOpaqueType ) ;
3049
+
3050
+ }
3049
3051
3050
3052
// TODO: If the definition type is a template class/struct
3051
3053
// definition's member template definition, it could rely on
@@ -3115,12 +3117,12 @@ impl CodeGenerator for Function {
3115
3117
// instantiations is open ended and we have no way of knowing which
3116
3118
// monomorphizations actually exist.
3117
3119
let type_params = item. all_template_params ( ctx) ;
3118
- if let Some ( params) = type_params {
3119
- if !params. is_empty ( ) {
3120
- return ;
3121
- }
3120
+
3121
+ if !type_params. is_empty ( ) {
3122
+ return ;
3122
3123
}
3123
3124
3125
+
3124
3126
let name = self . name ( ) ;
3125
3127
let mut canonical_name = item. canonical_name ( ctx) ;
3126
3128
let mangled_name = self . mangled_name ( ) ;
0 commit comments