feat(bindings/swift): add Swift binding#2470
Merged
Xuanwo merged 5 commits intoapache:mainfrom Jun 17, 2023
Merged
Conversation
Xuanwo
reviewed
Jun 16, 2023
unixzii
commented
Jun 16, 2023
Xuanwo
reviewed
Jun 17, 2023
Xuanwo
approved these changes
Jun 17, 2023
Member
Xuanwo
left a comment
There was a problem hiding this comment.
Thanks a lot! Let's go! Are you willing to create a tracking issue for not finished tasks?
Contributor
Author
Sure, I will create one later. |
6 tasks
Contributor
|
uniffi (https://github.com/mozilla/uniffi-rs) could be a slightly better solution for Swift binding |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Datatype in Swift for receiving data from Rust. And usewithUnsafeBytesto access the borrowed pointer toDatawhen 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:
tokioworks 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