diff --git a/examples/nim_timer/rust_bindings/Cargo.lock b/examples/nim_timer/rust_bindings/Cargo.lock new file mode 100644 index 0000000..86f2085 --- /dev/null +++ b/examples/nim_timer/rust_bindings/Cargo.lock @@ -0,0 +1,123 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "nimtimer" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "tokio", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +dependencies = [ + "pin-project-lite", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/examples/nim_timer/rust_bindings/src/api.rs b/examples/nim_timer/rust_bindings/src/api.rs index a9e88e8..009aa4b 100644 --- a/examples/nim_timer/rust_bindings/src/api.rs +++ b/examples/nim_timer/rust_bindings/src/api.rs @@ -19,17 +19,14 @@ unsafe extern "C" fn on_result( user_data: *mut c_void, ) { let pair = Arc::from_raw(user_data as *const (Mutex, Condvar)); - { - let (lock, cvar) = &*pair; - let mut state = lock.lock().unwrap(); - state.payload = Some(if ret == 0 { - Ok(CStr::from_ptr(msg).to_string_lossy().into_owned()) - } else { - Err(CStr::from_ptr(msg).to_string_lossy().into_owned()) - }); - cvar.notify_one(); - } - std::mem::forget(pair); + let (lock, cvar) = &*pair; + let mut state = lock.lock().unwrap(); + state.payload = Some(if ret == 0 { + Ok(CStr::from_ptr(msg).to_string_lossy().into_owned()) + } else { + Err(CStr::from_ptr(msg).to_string_lossy().into_owned()) + }); + cvar.notify_one(); } fn ffi_call(timeout: Duration, f: F) -> Result @@ -40,6 +37,7 @@ where let raw = Arc::into_raw(pair.clone()) as *mut c_void; let ret = f(on_result, raw); if ret == 2 { + drop(unsafe { Arc::from_raw(raw as *const (Mutex, Condvar)) }); return Err("RET_MISSING_CALLBACK (internal error)".into()); } let (lock, cvar) = &*pair; diff --git a/ffi/codegen/rust.nim b/ffi/codegen/rust.nim index b3c59d1..28e265f 100644 --- a/ffi/codegen/rust.nim +++ b/ffi/codegen/rust.nim @@ -265,17 +265,14 @@ proc generateApiRs*(procs: seq[FFIProcMeta], libName: string): string = lines.add(" user_data: *mut c_void,") lines.add(") {") lines.add(" let pair = Arc::from_raw(user_data as *const (Mutex, Condvar));") - lines.add(" {") - lines.add(" let (lock, cvar) = &*pair;") - lines.add(" let mut state = lock.lock().unwrap();") - lines.add(" state.payload = Some(if ret == 0 {") - lines.add(" Ok(CStr::from_ptr(msg).to_string_lossy().into_owned())") - lines.add(" } else {") - lines.add(" Err(CStr::from_ptr(msg).to_string_lossy().into_owned())") - lines.add(" });") - lines.add(" cvar.notify_one();") - lines.add(" }") - lines.add(" std::mem::forget(pair);") + lines.add(" let (lock, cvar) = &*pair;") + lines.add(" let mut state = lock.lock().unwrap();") + lines.add(" state.payload = Some(if ret == 0 {") + lines.add(" Ok(CStr::from_ptr(msg).to_string_lossy().into_owned())") + lines.add(" } else {") + lines.add(" Err(CStr::from_ptr(msg).to_string_lossy().into_owned())") + lines.add(" });") + lines.add(" cvar.notify_one();") lines.add("}") lines.add("") lines.add("fn ffi_call(timeout: Duration, f: F) -> Result") @@ -286,6 +283,7 @@ proc generateApiRs*(procs: seq[FFIProcMeta], libName: string): string = lines.add(" let raw = Arc::into_raw(pair.clone()) as *mut c_void;") lines.add(" let ret = f(on_result, raw);") lines.add(" if ret == 2 {") + lines.add(" drop(unsafe { Arc::from_raw(raw as *const (Mutex, Condvar)) });") lines.add(" return Err(\"RET_MISSING_CALLBACK (internal error)\".into());") lines.add(" }") lines.add(" let (lock, cvar) = &*pair;")