diff --git a/examples/nim_timer/rust_bindings/src/api.rs b/examples/nim_timer/rust_bindings/src/api.rs index 960431e..8be916e 100644 --- a/examples/nim_timer/rust_bindings/src/api.rs +++ b/examples/nim_timer/rust_bindings/src/api.rs @@ -114,14 +114,14 @@ impl NimTimerCtx { Ok(Self { ptr: addr as *mut c_void, timeout }) } - pub async fn new_async(config: TimerConfig) -> Result { + pub async fn new_async(config: TimerConfig, timeout: Duration) -> Result { let config_json = serde_json::to_string(&config).map_err(|e| e.to_string())?; let config_c = CString::new(config_json).unwrap(); let raw = ffi_call_async(move |cb, ud| unsafe { ffi::nimtimer_create(config_c.as_ptr(), cb, ud) }).await?; let addr: usize = raw.parse().map_err(|e: std::num::ParseIntError| e.to_string())?; - Ok(Self { ptr: addr as *mut c_void, timeout: Duration::from_secs(30) }) + Ok(Self { ptr: addr as *mut c_void, timeout }) } pub fn echo(&self, req: EchoRequest) -> Result { diff --git a/examples/nim_timer/rust_client/src/tokio_main.rs b/examples/nim_timer/rust_client/src/tokio_main.rs index 5303de8..1ff48e3 100644 --- a/examples/nim_timer/rust_client/src/tokio_main.rs +++ b/examples/nim_timer/rust_client/src/tokio_main.rs @@ -1,8 +1,13 @@ +use std::time::Duration; use nimtimer::{EchoRequest, NimTimerCtx, TimerConfig}; #[tokio::main(flavor = "multi_thread", worker_threads = 2)] async fn main() -> Result<(), Box> { - let ctx = NimTimerCtx::new_async(TimerConfig { name: "tokio-demo".into() }).await?; + let ctx = NimTimerCtx::new_async( + TimerConfig { name: "tokio-demo".into() }, + Duration::from_secs(30), + ) + .await?; let version = ctx.version_async().await?; println!("[1] Tokio runtime started"); diff --git a/ffi/codegen/rust.nim b/ffi/codegen/rust.nim index d489e41..5236dfc 100644 --- a/ffi/codegen/rust.nim +++ b/ffi/codegen/rust.nim @@ -367,8 +367,7 @@ proc generateApiRs*(procs: seq[FFIProcMeta], libName: string): string = asyncParamsList.add( "$1: $2" % [toSnakeCase(ep.name), nimTypeToRust(ep.typeName)] ) - let asyncParamsStr = asyncParamsList.join(", ") - let blockingParamsStr = + let paramsStr = if asyncParamsList.len > 0: asyncParamsList.join(", ") & ", timeout: Duration" else: "timeout: Duration" @@ -396,7 +395,7 @@ proc generateApiRs*(procs: seq[FFIProcMeta], libName: string): string = let ffiCallArgsStr = ffiCallArgs.join(", ") # -- blocking create -- - lines.add(" pub fn create($1) -> Result {" % [blockingParamsStr]) + lines.add(" pub fn create($1) -> Result {" % [paramsStr]) for ep in ctor.extraParams: emitSerialize(toSnakeCase(ep.name), nimTypeToRust(ep.typeName)) lines.add(" let raw = ffi_call(timeout, |cb, ud| unsafe {") @@ -409,14 +408,14 @@ proc generateApiRs*(procs: seq[FFIProcMeta], libName: string): string = # -- async new_async -- # move closure: each CString is moved in (Send), no raw ptr escapes the block - lines.add(" pub async fn new_async($1) -> Result {" % [asyncParamsStr]) + lines.add(" pub async fn new_async($1) -> Result {" % [paramsStr]) for ep in ctor.extraParams: emitSerialize(toSnakeCase(ep.name), nimTypeToRust(ep.typeName)) lines.add(" let raw = ffi_call_async(move |cb, ud| unsafe {") lines.add(" ffi::$1($2)" % [ctor.procName, ffiCallArgsStr]) lines.add(" }).await?;") lines.add(" let addr: usize = raw.parse().map_err(|e: std::num::ParseIntError| e.to_string())?;") - lines.add(" Ok(Self { ptr: addr as *mut c_void, timeout: Duration::from_secs(30) })") + lines.add(" Ok(Self { ptr: addr as *mut c_void, timeout })") lines.add(" }") lines.add("")