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
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:
Cons:
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:
Cons:
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:
Cons: