mozilla / uniffi-rs

a multi-language bindings generator for rust
https://mozilla.github.io/uniffi-rs/
Mozilla Public License 2.0
2.48k stars 211 forks source link

Use Data(bytesNoCopy:size:deallocator:) for conversion between RustBuffer and Data #2172

Closed Akazm closed 1 hour ago

Akazm commented 2 days ago

This avoids unnecessary copies between FFI and Swift.

Tests are apparently entirely unaffected (i.e. no changes to their results) by this change. deallocator is set to .none since the FfiConverterRustBuffer extension function lift(_:) ensures deallocation on it's own.

badboy commented 3 hours ago

Looks like we use Data in two places:

https://github.com/mozilla/uniffi-rs/blob/bfa98ed377629de931587cf412692f4388ca9471/uniffi_bindgen/src/bindings/swift/templates/ObjectTemplate.swift#L174

https://github.com/mozilla/uniffi-rs/blob/bfa98ed377629de931587cf412692f4388ca9471/uniffi_bindgen/src/bindings/swift/templates/RustBufferTemplate.swift#L173

Both times Data is created adhoc and the reader passed on as a reference. The Rust buffer is created sometime before those functions are called and is then deallocated shortly after. It therefore should be safe to have Data not copy out the data, but just reference it.