uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

Ability to share data across FFI without a copy

Open jfro opened this issue 1 year ago • 4 comments

We've been exploring using UniFFI for our updated version of a core library to make things easier on us than using straight C FFI code & hand written wrappers. Problem is we have some parts of the code where we'd want to minimize overhead due to low latency video use cases. So ideally when sharing something like an object (interface struct with a Vec<u8> inside for example) we'd want to be able to get the ptr/len to let the foreign swift/etc code do the final copy, avoiding the copy happening at FFI level as well.

jfro avatar Jan 31 '24 19:01 jfro

Just got here from #1485. @mhammond, can it be assumed from the comments on that issue that there aren't any plans to allow data sharing across FFI without copy?

cadnza avatar Apr 11 '24 17:04 cadnza

I'm also working on a video-related app and ran into this, and it's fairly painful.

If I leave 99% of my API using UniFFI, is there a way to drop down to C calling convention for the last 1%? In other words, if I define a

#[no_mangle]
pub extern "C" fn get_unsafe_data_pointer(x: ...) ->  *const c_void {
    todo!()
} 

What can I use for the type of x if I want to pass back in a UniFFI handle to a uniffi::object that I passed over foreign code? And is it different per language?

colinmarc avatar Aug 17 '24 20:08 colinmarc

you can try using uniffiClonePointer on the uniffi type to get a raw ptr of the Rust type that you can pass to a traditional FFI rust call, we're doing this for low overhead bits of our library.

jfro avatar Aug 23 '24 16:08 jfro