@@ -226,41 +226,42 @@ impl Rusty for LvFunc {
226
226
}
227
227
} ) ;
228
228
229
- // TODO Unsafe function for getters should be lvgl_sys::lv_bar_get_value(self.core.raw().as_ptr()) ?
230
- // Currently they are lvgl_sys :: lv_bar_get_value (self . core . raw () . as_mut ()) }
231
- let args_call = self
229
+ // Generate the arguments being passed into the FFI interface
230
+ //
231
+ // - The first argument will be always self.core.raw().as_mut() (see quote! when arg_idx == 0), it's most likely a pointer to lv_obj_t
232
+ // TODO: When handling getters this should be self.raw().as_ptr() instead, this also requires updating args_decl
233
+ // - The arguments will be appended to the accumulator (args_accumulator) as they are generated in the closure
234
+ let ffi_args = self
232
235
. args
233
236
. iter ( )
234
237
. enumerate ( )
235
- . fold ( quote ! ( ) , |args, ( i, arg) | {
236
- // if first arg is `const`, then it should be immutable
237
- let next_arg = if i == 0 {
238
+ . fold ( quote ! ( ) , |args_accumulator, ( arg_idx, arg) | {
239
+ let next_arg = if arg_idx == 0 {
238
240
quote ! ( self . core. raw( ) . as_mut( ) )
239
241
} else {
240
242
let var = arg. get_value_usage ( ) ;
241
243
quote ! ( #var)
242
244
} ;
243
- if args . is_empty ( ) {
244
- quote ! {
245
- #next_arg
246
- }
247
- } else {
248
- quote ! {
249
- #args , #next_arg
250
- }
245
+
246
+ // If the accummulator is empty then we call quote! only with the next_arg content
247
+ if args_accumulator . is_empty ( ) {
248
+ quote ! { #next_arg }
249
+ }
250
+ // Otherwise we append next_arg at the end of the accumulator
251
+ else {
252
+ quote ! { #args_accumulator , #next_arg }
251
253
}
252
254
} ) ;
253
255
254
- // NOTE: When the function returns something we can 'avoid' placing an Ok()
255
- // at the end.
256
- let return_ok_at_the_end = if return_type. is_empty ( ) {
256
+ // NOTE: When the function returns something we can 'avoid' placing an Ok() at the end.
257
+ let explicit_ok = if return_type. is_empty ( ) {
257
258
quote ! ( Ok ( ( ) ) )
258
259
} else {
259
260
quote ! ( )
260
261
} ;
261
262
262
263
// And we can also return from the unsafe block by removing the ; at the end
263
- let implicit_return = if has_return_value {
264
+ let optional_semicolon = if has_return_value {
264
265
quote ! ( )
265
266
} else {
266
267
quote ! ( ; )
@@ -270,10 +271,10 @@ impl Rusty for LvFunc {
270
271
pub fn #func_name( #args_decl) -> #return_type {
271
272
#args_processing
272
273
unsafe {
273
- lvgl_sys:: #original_func_name( #args_call ) #implicit_return
274
+ lvgl_sys:: #original_func_name( #ffi_args ) #optional_semicolon
274
275
}
275
276
276
- #return_ok_at_the_end
277
+ #explicit_ok
277
278
}
278
279
} )
279
280
}
0 commit comments