Skip to content

Commit eb2b4ca

Browse files
committed
codegen: Add comments and renaming variables for clarity
1 parent 52ad533 commit eb2b4ca

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

lvgl-codegen/src/lib.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -226,41 +226,42 @@ impl Rusty for LvFunc {
226226
}
227227
});
228228

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
232235
.args
233236
.iter()
234237
.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 {
238240
quote!(self.core.raw().as_mut())
239241
} else {
240242
let var = arg.get_value_usage();
241243
quote!(#var)
242244
};
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}
251253
}
252254
});
253255

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() {
257258
quote!(Ok(()))
258259
} else {
259260
quote!()
260261
};
261262

262263
// 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 {
264265
quote!()
265266
} else {
266267
quote!(;)
@@ -270,10 +271,10 @@ impl Rusty for LvFunc {
270271
pub fn #func_name(#args_decl) -> #return_type {
271272
#args_processing
272273
unsafe {
273-
lvgl_sys::#original_func_name(#args_call)#implicit_return
274+
lvgl_sys::#original_func_name(#ffi_args)#optional_semicolon
274275
}
275276

276-
#return_ok_at_the_end
277+
#explicit_ok
277278
}
278279
})
279280
}

0 commit comments

Comments
 (0)