For closure arguments to synchronous commands which has `cbkind`
`CBOnceCommand` or `CBManyCommand`, the requirement of the `'static`
lifetime is removed. This greatly simplifies the API for the user as the
user might otherwise have to use both an `Arc` and a `Mutex` when
calling for instance `opt_list()`.
---
generator/Rust.ml | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/generator/Rust.ml b/generator/Rust.ml
index 37582ac..96095a9 100644
--- a/generator/Rust.ml
+++ b/generator/Rust.ml
@@ -113,7 +113,7 @@ let rust_cbarg_name : cbarg -> string = function
| CBArrayAndLen (arg, _) | CBMutable arg -> rust_arg_name arg
(* Get the Rust type for an argument. *)
-let rec rust_arg_type : arg -> string = function
+let rec rust_arg_type ?(async_kind = None) : arg -> string = function
| Bool _ -> "bool"
| Int _ -> "c_int"
| UInt _ -> "c_uint"
@@ -133,7 +133,10 @@ let rec rust_arg_type : arg -> string = function
| BytesOut _ -> "&mut [u8]"
| BytesPersistIn _ -> "&'static [u8]"
| BytesPersistOut _ -> "&'static mut [u8]"
- | Closure { cbargs; cbkind } -> "impl " ^ rust_closure_trait cbargs
cbkind
+ | Closure { cbargs; cbkind } -> (
+ match async_kind with
+ | Some _ -> "impl " ^ rust_closure_trait cbargs cbkind
+ | None -> "impl " ^ rust_closure_trait cbargs cbkind ~lifetime:None)
(* Get the Rust closure trait for a callback, That is `Fn*(...) -> ...)`. *)
and rust_closure_trait ?(lifetime = Some "'static") cbargs cbkind : string
=
@@ -160,8 +163,8 @@ and rust_cbarg_type : cbarg -> string = function
| CBMutable arg -> "&mut " ^ rust_arg_type arg
(* Get the type of a rust optional argument. *)
-let rust_optarg_type : optarg -> string = function
- | OClosure x -> sprintf "Option<%s>" (rust_arg_type (Closure x))
+let rust_optarg_type ?(async_kind = None) : optarg -> string = function
+ | OClosure x -> sprintf "Option<%s>" (rust_arg_type (Closure x)
~async_kind)
| OFlags (name, flags, _) ->
sprintf "Option<%s>" (rust_arg_type (Flags (name, flags)))
@@ -549,7 +552,8 @@ let print_rust_handle_method ((name, call) : string * call) =
let rust_args_names =
List.map rust_arg_name call.args @ List.map rust_optarg_name call.optargs
and rust_args_types =
- List.map rust_arg_type call.args @ List.map rust_optarg_type call.optargs
+ List.map (rust_arg_type ~async_kind:call.async_kind) call.args
+ @ List.map (rust_optarg_type ~async_kind:call.async_kind) call.optargs
in
let rust_args =
String.concat ", "
--
2.41.0