@@ -9,7 +9,7 @@ use crate::generator::default_parameters;
9
9
use crate :: models:: domain:: { FnParam , FnQualifier , Function , RustTy } ;
10
10
use crate :: special_cases;
11
11
use crate :: util:: safe_ident;
12
- use proc_macro2:: TokenStream ;
12
+ use proc_macro2:: { Ident , TokenStream } ;
13
13
use quote:: { format_ident, quote} ;
14
14
15
15
pub struct FnReceiver {
@@ -84,6 +84,22 @@ impl FnDefinitions {
84
84
}
85
85
}
86
86
87
+ // Gathers multiple token vectors related to function parameters.
88
+ #[ derive( Default ) ]
89
+ pub struct FnParamTokens {
90
+ pub params : Vec < TokenStream > ,
91
+ pub param_types : Vec < TokenStream > ,
92
+ pub arg_names : Vec < TokenStream > ,
93
+ }
94
+
95
+ impl FnParamTokens {
96
+ pub fn push_regular ( & mut self , param_name : & Ident , param_ty : & RustTy ) {
97
+ self . params . push ( quote ! { #param_name: #param_ty } ) ;
98
+ self . arg_names . push ( quote ! { #param_name } ) ;
99
+ self . param_types . push ( quote ! { #param_ty } ) ;
100
+ }
101
+ }
102
+
87
103
pub fn make_function_definition (
88
104
sig : & dyn Function ,
89
105
code : & FnCode ,
@@ -120,7 +136,11 @@ pub fn make_function_definition(
120
136
( TokenStream :: new ( ) , TokenStream :: new ( ) )
121
137
} ;
122
138
123
- let [ params, param_types, arg_names] = if sig. is_virtual ( ) {
139
+ let FnParamTokens {
140
+ params,
141
+ param_types,
142
+ arg_names,
143
+ } = if sig. is_virtual ( ) {
124
144
make_params_exprs_virtual ( sig. params ( ) . iter ( ) , sig)
125
145
} else {
126
146
make_params_exprs (
@@ -204,7 +224,10 @@ pub fn make_function_definition(
204
224
// This can be made more complex if ever necessary.
205
225
206
226
// A function() may call try_function(), its arguments should not have .as_object_arg().
207
- let [ _, _, arg_names_without_asarg] = make_params_exprs (
227
+ let FnParamTokens {
228
+ arg_names : arg_names_without_asarg,
229
+ ..
230
+ } = make_params_exprs (
208
231
sig. params ( ) . iter ( ) ,
209
232
!has_default_params, // For *_full function, we don't need impl AsObjectArg<T> parameters
210
233
false , // or arg.as_object_arg() calls.
@@ -315,10 +338,8 @@ pub(crate) fn make_params_exprs<'a>(
315
338
method_args : impl Iterator < Item = & ' a FnParam > ,
316
339
param_is_impl_asarg : bool ,
317
340
arg_is_asarg : bool ,
318
- ) -> [ Vec < TokenStream > ; 3 ] {
319
- let mut params = vec ! [ ] ;
320
- let mut param_types = vec ! [ ] ; // or non-generic params
321
- let mut arg_names = vec ! [ ] ;
341
+ ) -> FnParamTokens {
342
+ let mut ret = FnParamTokens :: default ( ) ;
322
343
323
344
for param in method_args {
324
345
let param_name = & param. name ;
@@ -333,41 +354,35 @@ pub(crate) fn make_params_exprs<'a>(
333
354
} => {
334
355
// Parameter declarations in signature: impl AsObjectArg<T>
335
356
if param_is_impl_asarg {
336
- params. push ( quote ! { #param_name: #impl_as_object_arg } ) ;
357
+ ret . params . push ( quote ! { #param_name: #impl_as_object_arg } ) ;
337
358
} else {
338
- params. push ( quote ! { #param_name: #object_arg } ) ;
359
+ ret . params . push ( quote ! { #param_name: #object_arg } ) ;
339
360
}
340
361
341
362
// Argument names in function body: arg.as_object_arg() vs. arg
342
363
if arg_is_asarg {
343
- arg_names. push ( quote ! { #param_name. as_object_arg( ) } ) ;
364
+ ret . arg_names . push ( quote ! { #param_name. as_object_arg( ) } ) ;
344
365
} else {
345
- arg_names. push ( quote ! { #param_name } ) ;
366
+ ret . arg_names . push ( quote ! { #param_name } ) ;
346
367
}
347
368
348
- param_types. push ( quote ! { #object_arg } ) ;
369
+ ret . param_types . push ( quote ! { #object_arg } ) ;
349
370
}
350
371
351
372
// All other methods and parameter types: standard handling.
352
- _ => {
353
- params. push ( quote ! { #param_name: #param_ty } ) ;
354
- arg_names. push ( quote ! { #param_name } ) ;
355
- param_types. push ( quote ! { #param_ty } ) ;
356
- }
373
+ _ => ret. push_regular ( param_name, param_ty) ,
357
374
}
358
375
}
359
376
360
- [ params , param_types , arg_names ]
377
+ ret
361
378
}
362
379
363
380
/// For virtual functions, returns the parameter declarations, type tokens, and names.
364
381
pub ( crate ) fn make_params_exprs_virtual < ' a > (
365
382
method_args : impl Iterator < Item = & ' a FnParam > ,
366
383
function_sig : & dyn Function ,
367
- ) -> [ Vec < TokenStream > ; 3 ] {
368
- let mut params = vec ! [ ] ;
369
- let mut param_types = vec ! [ ] ; // or non-generic params
370
- let mut arg_names = vec ! [ ] ;
384
+ ) -> FnParamTokens {
385
+ let mut ret = FnParamTokens :: default ( ) ;
371
386
372
387
for param in method_args {
373
388
let param_name = & param. name ;
@@ -382,21 +397,17 @@ pub(crate) fn make_params_exprs_virtual<'a>(
382
397
param_name,
383
398
) =>
384
399
{
385
- params. push ( quote ! { #param_name: Option <#param_ty> } ) ;
386
- arg_names. push ( quote ! { #param_name } ) ;
387
- param_types. push ( quote ! { #param_ty } ) ;
400
+ ret . params . push ( quote ! { #param_name: Option <#param_ty> } ) ;
401
+ ret . arg_names . push ( quote ! { #param_name } ) ;
402
+ ret . param_types . push ( quote ! { #param_ty } ) ;
388
403
}
389
404
390
405
// All other methods and parameter types: standard handling.
391
- _ => {
392
- params. push ( quote ! { #param_name: #param_ty } ) ;
393
- arg_names. push ( quote ! { #param_name } ) ;
394
- param_types. push ( quote ! { #param_ty } ) ;
395
- }
406
+ _ => ret. push_regular ( param_name, param_ty) ,
396
407
}
397
408
}
398
409
399
- [ params , param_types , arg_names ]
410
+ ret
400
411
}
401
412
402
413
fn function_uses_pointers ( sig : & dyn Function ) -> bool {
0 commit comments