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

Rust -> Swift interop #18

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

lucasmerlin
Copy link
Contributor

Because I was curious myself what the best way to call swift would be I've implemented three different methods:

use @_cdecl to export swift functions and call them from rust via extern "C"

Pros:

  • Easy to implement
  • Works great for simple function calls without arguments

Cons:

  • Sharing anything more than ints and floats gets really complicated and will need lots of unsafe

via objc using objc2 crate

We can declare a objc "class" in some objc glue code and use the objc2::extern_class macro to declare it in rust.
Then we can call the swift code from our objective c code.

Pros:

  • winit uses objc2 so there is a lot of example code
  • Sharing complex data structures is possible and relatively easy
  • icrate has rust bindings for a lot of ios apis

Cons:

  • Needs a intermediary objc layer in the xcode project, can't call swift directly.

via swift-rs

We can create a swift package containing our ios swift code and link it to our rust code via swift-rs

Pros:

  • No objc layer necessary
  • Allows using strings and structs as args

Cons:

  • iOS code needs to live in a separate Swift Package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant