@@ -284,20 +284,21 @@ impl AppendImplicitTemplateParams for quote::Tokens {
284
284
_ => { } ,
285
285
}
286
286
287
- if let Some ( params) = item. used_template_params ( ctx) {
288
- if params. is_empty ( ) {
289
- return ;
290
- }
287
+ let params = item. used_template_params ( ctx) ;
291
288
292
- let params = params. into_iter ( ) . map ( |p| {
293
- p. try_to_rust_ty ( ctx, & ( ) )
294
- . expect ( "template params cannot fail to be a rust type" )
295
- } ) ;
296
-
297
- self . append ( quote ! {
298
- < #( #params ) , * >
299
- } ) ;
289
+ if params. is_empty ( ) {
290
+ return ;
300
291
}
292
+
293
+ let params = params. into_iter ( ) . map ( |p| {
294
+ p. try_to_rust_ty ( ctx, & ( ) )
295
+ . expect ( "template params cannot fail to be a rust type" )
296
+ } ) ;
297
+
298
+ self . append ( quote ! {
299
+ < #( #params ) , * >
300
+ } ) ;
301
+
301
302
}
302
303
}
303
304
@@ -461,11 +462,9 @@ impl CodeGenerator for Var {
461
462
// We can't generate bindings to static variables of templates. The
462
463
// number of actual variables for a single declaration are open ended
463
464
// and we don't know what instantiations do or don't exist.
464
- let type_params = item. all_template_params ( ctx) ;
465
- if let Some ( params) = type_params {
466
- if !params. is_empty ( ) {
467
- return ;
468
- }
465
+
466
+ if !item. all_template_params ( ctx) . is_empty ( ) {
467
+ return
469
468
}
470
469
471
470
let ty = self . ty ( ) . to_rust_ty_or_opaque ( ctx, & ( ) ) ;
@@ -619,15 +618,15 @@ impl CodeGenerator for Type {
619
618
return ;
620
619
}
621
620
622
- let mut outer_params = item . used_template_params ( ctx )
623
- . and_then ( |ps| if ps . is_empty ( ) {
624
- None
621
+ let mut outer_params =
622
+ if !item . used_template_params ( ctx ) . is_empty ( ) {
623
+ item . used_template_params ( ctx ) . clone ( )
625
624
} else {
626
- Some ( ps )
627
- } ) ;
625
+ Vec :: new ( )
626
+ } ;
628
627
629
628
let inner_rust_type = if item. is_opaque ( ctx, & ( ) ) {
630
- outer_params = None ;
629
+ outer_params = Vec :: new ( ) ;
631
630
self . to_opaque ( ctx, item)
632
631
} else {
633
632
// Its possible that we have better layout information than
@@ -682,7 +681,7 @@ impl CodeGenerator for Type {
682
681
'A' ...'Z' | 'a' ...'z' | '0' ...'9' | ':' | '_' | ' ' => true ,
683
682
_ => false ,
684
683
} ) &&
685
- outer_params. is_none ( ) &&
684
+ outer_params. is_empty ( ) &&
686
685
inner_item. expect_type ( ) . canonical_type ( ctx) . is_enum ( )
687
686
{
688
687
tokens. append ( quote ! {
@@ -701,8 +700,8 @@ impl CodeGenerator for Type {
701
700
pub type #rust_name
702
701
} ) ;
703
702
704
- if let Some ( params ) = outer_params {
705
- let params: Vec < _ > = params . into_iter ( )
703
+ if !outer_params . is_empty ( ) {
704
+ let params: Vec < _ > = outer_params . into_iter ( )
706
705
. filter_map ( |p| p. as_template_param ( ctx, & ( ) ) )
707
706
. collect ( ) ;
708
707
if params. iter ( ) . any ( |p| ctx. resolve_type ( * p) . is_invalid_type_param ( ) ) {
@@ -1401,7 +1400,7 @@ impl CodeGenerator for CompInfo {
1401
1400
//
1402
1401
// NB: We generate a proper struct to avoid struct/function name
1403
1402
// collisions.
1404
- if self . is_forward_declaration ( ) && used_template_params. is_none ( ) {
1403
+ if self . is_forward_declaration ( ) && used_template_params. is_empty ( ) {
1405
1404
let struct_name = item. canonical_name ( ctx) ;
1406
1405
let struct_name = ctx. rust_ident_raw ( struct_name) ;
1407
1406
let tuple_struct = quote ! {
@@ -1584,7 +1583,9 @@ impl CodeGenerator for CompInfo {
1584
1583
1585
1584
let mut generic_param_names = vec ! [ ] ;
1586
1585
1587
- if let Some ( ref params) = used_template_params {
1586
+ let ref params = used_template_params;
1587
+
1588
+ if !params. is_empty ( ) {
1588
1589
for ( idx, ty) in params. iter ( ) . enumerate ( ) {
1589
1590
let param = ctx. resolve_type ( * ty) ;
1590
1591
let name = param. name ( ) . unwrap ( ) ;
@@ -1594,13 +1595,14 @@ impl CodeGenerator for CompInfo {
1594
1595
let prefix = ctx. trait_prefix ( ) ;
1595
1596
let field_name = ctx. rust_ident ( format ! ( "_phantom_{}" , idx) ) ;
1596
1597
fields. push ( quote ! {
1597
- pub #field_name : :: #prefix:: marker:: PhantomData <
1598
- :: #prefix:: cell:: UnsafeCell <#ident>
1599
- > ,
1600
- } ) ;
1598
+ pub #field_name : :: #prefix:: marker:: PhantomData <
1599
+ :: #prefix:: cell:: UnsafeCell <#ident>
1600
+ > ,
1601
+ } ) ;
1601
1602
}
1602
1603
}
1603
1604
1605
+
1604
1606
let generics = if !generic_param_names. is_empty ( ) {
1605
1607
let generic_param_names = generic_param_names. clone ( ) ;
1606
1608
quote ! {
@@ -1642,7 +1644,7 @@ impl CodeGenerator for CompInfo {
1642
1644
ctx. options ( ) . derive_copy
1643
1645
{
1644
1646
derives. push ( "Copy" ) ;
1645
- if used_template_params. is_some ( ) {
1647
+ if ! used_template_params. is_empty ( ) {
1646
1648
// FIXME: This requires extra logic if you have a big array in a
1647
1649
// templated struct. The reason for this is that the magic:
1648
1650
// fn clone(&self) -> Self { *self }
@@ -1726,7 +1728,7 @@ impl CodeGenerator for CompInfo {
1726
1728
) ;
1727
1729
}
1728
1730
1729
- if used_template_params. is_none ( ) {
1731
+ if used_template_params. is_empty ( ) {
1730
1732
if !is_opaque {
1731
1733
for var in self . inner_vars ( ) {
1732
1734
ctx. resolve_item ( * var) . codegen ( ctx, result, & ( ) ) ;
@@ -2928,10 +2930,10 @@ impl TryToRustTy for Type {
2928
2930
TypeKind :: TemplateAlias ( ..) |
2929
2931
TypeKind :: Alias ( ..) => {
2930
2932
let template_params = item. used_template_params ( ctx)
2931
- . unwrap_or ( vec ! [ ] )
2932
- . into_iter ( )
2933
- . filter ( |param| param. is_template_param ( ctx, & ( ) ) )
2934
- . collect :: < Vec < _ > > ( ) ;
2933
+
2934
+ . into_iter ( )
2935
+ . filter ( |param| param. is_template_param ( ctx, & ( ) ) )
2936
+ . collect :: < Vec < _ > > ( ) ;
2935
2937
2936
2938
let spelling = self . name ( ) . expect ( "Unnamed alias?" ) ;
2937
2939
if item. is_opaque ( ctx, & ( ) ) && !template_params. is_empty ( ) {
@@ -2949,7 +2951,7 @@ impl TryToRustTy for Type {
2949
2951
TypeKind :: Comp ( ref info) => {
2950
2952
let template_params = item. used_template_params ( ctx) ;
2951
2953
if info. has_non_type_template_params ( ) ||
2952
- ( item. is_opaque ( ctx, & ( ) ) && template_params. is_some ( ) )
2954
+ ( item. is_opaque ( ctx, & ( ) ) && ! template_params. is_empty ( ) )
2953
2955
{
2954
2956
return self . try_to_opaque ( ctx, item) ;
2955
2957
}
@@ -3043,18 +3045,18 @@ impl TryToRustTy for TemplateInstantiation {
3043
3045
let def_path = def. namespace_aware_canonical_path ( ctx) ;
3044
3046
ty. append_separated ( def_path. into_iter ( ) . map ( |p| ctx. rust_ident ( p) ) , "::" ) ;
3045
3047
3046
- let def_params = match def. self_template_params ( ctx) {
3047
- Some ( params ) => params ,
3048
- None => {
3049
- // This can happen if we generated an opaque type for a partial
3050
- // template specialization, and we've hit an instantiation of
3051
- // that partial specialization.
3052
- extra_assert ! (
3053
- def. is_opaque( ctx, & ( ) )
3054
- ) ;
3055
- return Err ( error:: Error :: InstantiationOfOpaqueType ) ;
3056
- }
3057
- } ;
3048
+ let def_params = def. self_template_params ( ctx) ;
3049
+
3050
+ if def_params . is_empty ( ) {
3051
+ // This can happen if we generated an opaque type for a partial
3052
+ // template specialization, and we've hit an instantiation of
3053
+ // that partial specialization.
3054
+ extra_assert ! (
3055
+ def. is_opaque( ctx, & ( ) )
3056
+ ) ;
3057
+ return Err ( error:: Error :: InstantiationOfOpaqueType ) ;
3058
+
3059
+ }
3058
3060
3059
3061
// TODO: If the definition type is a template class/struct
3060
3062
// definition's member template definition, it could rely on
@@ -3132,12 +3134,12 @@ impl CodeGenerator for Function {
3132
3134
// instantiations is open ended and we have no way of knowing which
3133
3135
// monomorphizations actually exist.
3134
3136
let type_params = item. all_template_params ( ctx) ;
3135
- if let Some ( params) = type_params {
3136
- if !params. is_empty ( ) {
3137
- return ;
3138
- }
3137
+
3138
+ if !type_params. is_empty ( ) {
3139
+ return ;
3139
3140
}
3140
3141
3142
+
3141
3143
let name = self . name ( ) ;
3142
3144
let mut canonical_name = item. canonical_name ( ctx) ;
3143
3145
let mangled_name = self . mangled_name ( ) ;
0 commit comments