Skip to content
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

Merged
merged 5 commits into from
Jun 17, 2023
Merged

Conversation

unixzii
Copy link
Contributor

@unixzii unixzii commented Jun 16, 2023

Don't merge! It's still working in progress.

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 use withUnsafeBytes to access the borrowed pointer to Data 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:

  • Determine the async runtime in Rust (tokio works fine here and it’s able to provide an environment to be used in other languages).
  • Expose async APIs in C binding.
  • Transform callback-based APIs into async APIs in Swift.

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:

  • Research Swift package plugins to see whether it‘s suitable for our use case.
  • Use git submodule to make the Rust library a binary vendor. (Can be hard to maintain)
  • Use xcframework to deliver a prebuilt package. (Only usable from Apple platforms and also hard to maintain)

Checklist

  • Complete the binding APIs
  • Refine package structure
  • Add tests and CI actions

@unixzii unixzii changed the title feat(bindings/swift): add Swift binding (WIP) [WIP] feat(bindings/swift): add Swift binding Jun 16, 2023
bindings/c/Cargo.toml Outdated Show resolved Hide resolved
bindings/swift/Sources/OpenDAL/Operator.swift Outdated Show resolved Hide resolved
bindings/swift/Sources/OpenDAL/Operator.swift Outdated Show resolved Hide resolved
bindings/swift/Sources/OpenDAL/Operator.swift Outdated Show resolved Hide resolved
Copy link
Member

@Xuanwo Xuanwo left a 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?

@Xuanwo Xuanwo marked this pull request as ready for review June 17, 2023 12:54
@Xuanwo Xuanwo merged commit b248c90 into apache:main Jun 17, 2023
@unixzii
Copy link
Contributor Author

unixzii commented Jun 17, 2023

Thanks a lot! Let's go! Are you willing to create a tracking issue for not finished tasks?

Sure, I will create one later.

@unixzii unixzii mentioned this pull request Jun 17, 2023
6 tasks
@suyanhanx suyanhanx changed the title [WIP] feat(bindings/swift): add Swift binding feat(bindings/swift): add Swift binding Jun 17, 2023
@imWildCat
Copy link
Contributor

uniffi (https://github.com/mozilla/uniffi-rs) could be a slightly better solution for Swift binding

@PsiACE PsiACE mentioned this pull request Jun 27, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants