Skip to content

Rust -> Swift interop#18

Open
lucasmerlin wants to merge 15 commits intogeph-official:masterfrom
lucasmerlin:geph-ios-call-swift
Open

Rust -> Swift interop#18
lucasmerlin wants to merge 15 commits intogeph-official:masterfrom
lucasmerlin:geph-ios-call-swift

Conversation

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