Skip to content

feat(bindings/swift): add Swift binding#2470

Merged
Xuanwo merged 5 commits intoapache:mainfrom
unixzii:swift-binding
Jun 17, 2023
Merged

feat(bindings/swift): add Swift binding#2470
Xuanwo merged 5 commits intoapache:mainfrom
unixzii:swift-binding

Conversation

@unixzii
Copy link
Copy Markdown
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

@github-actions github-actions Bot added the releases-note/feat The PR implements a new feature or has a title that begins with "feat" label Jun 16, 2023
@unixzii unixzii changed the title feat(bindings/swift): add Swift binding (WIP) [WIP] feat(bindings/swift): add Swift binding Jun 16, 2023
Comment thread bindings/c/Cargo.toml
Comment thread bindings/swift/Sources/COpenDAL/module.modulemap
Comment thread bindings/swift/Sources/OpenDAL/Operator.swift Outdated
Comment thread bindings/swift/Sources/OpenDAL/Operator.swift Outdated
Comment thread bindings/swift/Sources/OpenDAL/Operator.swift Outdated
Comment thread bindings/swift/Sources/OpenDAL/Operator.swift Outdated
Comment thread bindings/swift/Sources/OpenDAL/Data+OpenDAL.swift
Comment thread bindings/swift/Sources/OpenDAL/Operator.swift
Comment thread bindings/swift/README.md
Copy link
Copy Markdown
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
Copy Markdown
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
@MikeChongCan
Copy link
Copy Markdown
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

Labels

releases-note/feat The PR implements a new feature or has a title that begins with "feat"

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants