uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

async rust calls swift?

Open czarop opened this issue 4 months ago • 5 comments

Hi, I couldn't see this in the guide, so assuming it's a no, but worth asking!

I have a use case where I'd like to call Swift and Kotlin from a rust application. Is this possible, especially with async, using uniffi? I can see how you might implement a set of exposed functions in a static swift library which trigger callbacks to pass data to rust, but is it possible in this crate to have full async/await functionality going in this direction?

Many thanks!

czarop avatar Sep 02 '25 15:09 czarop

Is this what you're looking for? https://mozilla.github.io/uniffi-rs/0.29/futures.html

pronebird avatar Sep 10 '25 07:09 pronebird

don't believe so, this is for calling async rust fn from another language, i want the other way around - calling async swift fn from rust. I'm making a pure rust project, i just need to tap into a couple of native api's in swift. I've done smth similar with swift-bridge but it uses callback fnonce closures, not true async/await.

czarop avatar Sep 10 '25 14:09 czarop

What happens if you define a method that accepts async fn in rust. Does it translate to async fn in Swift bindings?

pronebird avatar Sep 10 '25 15:09 pronebird

Callback interfaces can have async methods - https://mozilla.github.io/uniffi-rs/latest/futures.html#exporting-async-trait-methods - which I think is roughly what you are asking for?

mhammond avatar Sep 10 '25 17:09 mhammond

Agree i think this is possible, there is a little added complexity from the fact that uniffi is set up to be driven from the non-rust languages with callbacks or trait objects being passed to rust to allow rust to talk back the other way. In the case of a rust app initiating this process, there isn't any built in way to kick things off from rust. It should be possible with some home-rolled ffi to trigger e.g. rust calls swift exposed fn, swift passes back the trait object rust needs to call swift fn's on, then proceed as per the examples.

czarop avatar Sep 13 '25 17:09 czarop