Skip to content

Asynchronous functions / callback support #31

@caoimhebyrne

Description

@caoimhebyrne

It would be amazing if we could call asynchronous Swift functions from Rust.

The only problem is: _@cdecl doesn't allow us to mark our functions as asynchronous:

src-swift/lib.swift

@_cdecl("my_async_fn") // ERROR: _@cdecl global function cannot be asynchronous
func myAsyncFunc() async {
    print("Hello, world!")
}

Since that's not possible, it would be nice if we could have better support for closures:

src-swift/lib.swift

@_cdecl("my_closure_func")
func myClosureFunc(closure: @escaping @convention(c) () -> Void) {
    closure()
}

src/main.rs

use swift_rs::{swift, SRClosure};

swift!(pub(crate) fn my_closure_func(SRClosure<Void>));

fn main() {
    let closure: SRClosure<Void> = || {
        println!("Hello!!");
    }.into();

    my_closure_func(closure);
}

The Rust code above is just a proof-of-concept, and I'm not sure if everything I described there is possible, since we can't just pass a pointer to the callback around (from my understanding at least). We may have to implement something like this C-callbacks example from the Rustonomicon (with some sort of Swift code generator?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions