diff options
| author | Teddy Wing | 2018-08-26 13:30:09 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-08-26 13:37:16 +0200 |
| commit | 97a19adb5a2e53a86ba5505795f505117d2fa7b6 (patch) | |
| tree | 5951160da4819bc02b9c2c37734007eb94453eb9 | |
| parent | 2a02c11a3a33e9f8f77dcdda17a963c1ef782bf3 (diff) | |
| download | dome-key-map-97a19adb5a2e53a86ba5505795f505117d2fa7b6.tar.bz2 | |
c_run_key_action(): Correctly send action string to C
Using `.as_ptr()` on the `CString` didn't work. The C code wouldn't
print anything.
Turns out I needed to use `.into_raw()` in order to:
> [Consume] the CString and [transfer] ownership of the string to a C
> caller.
(https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_raw)
This allows us to correctly access and print the string in our C code.
Note that we'll still need to free the string using a new Rust function
that should be callable from C, otherwise we'll end up with a memory
leak.
| -rw-r--r-- | src/cocoa_bridge.rs | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index f33a739..49fc9f2 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -69,7 +69,7 @@ pub extern "C" fn c_run_key_action( match k.action { Some(a) => { CKeyActionResult { - action: CStr::from_bytes_with_nul(b"test?\n\0").unwrap().as_ptr(), + action: a.into_raw(), // memory leak, must be freed from Rust kind: &k.kind, } }, |
