-
Notifications
You must be signed in to change notification settings - Fork 509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(bindings/swift): add Swift binding #2470
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! Let's go! Are you willing to create a tracking issue for not finished tasks?
Sure, I will create one later. |
uniffi (https://github.com/mozilla/uniffi-rs) could be a slightly better solution for Swift binding |
This PR adds a new binding for Swift.
Implementation Details
Currently, the best way to integrate foreign-language libraries in Swift is via C APIs (FFI). We need to first bridge the Rust APIs to C APIs, and import the C header files in Swift with some proper linking settings.
We reused the C binding of OpenDAL, and import it directly in the Swift package. This can greatly share the same features that the C binding has already existed, and make code of the Swift binding as simple as possible (because it's just a wrapper).
To transfer large buffers between Swift and Rust code, we use the zero-copy initializer of
Data
type in Swift for receiving data from Rust. And usewithUnsafeBytes
to access the borrowed pointer toData
when sending data to Rust.Future Directions
Asynchronous Operations
Swift Concurrency provides async-await feature and now it’s stable. We can embrace it to deliver a streamlined DX as the Rust crate per se does.
To make it, we have some prerequisites:
tokio
works fine here and it’s able to provide an environment to be used in other languages).Better Integration of Rust Binary
Swift package users typically don’t perform extra actions before using the package. But it’s required for us because we need to build the Rust crate as a static library. It’s better to eliminate this manual operation for users.
Some possible approaches are:
Checklist