swift-bridge icon indicating copy to clipboard operation
swift-bridge copied to clipboard

Support Box<FnOnce> in extern "Rust"

Open daytime-em opened this issue 1 year ago • 3 comments

When you declare FnOnce on a Swift type, it doesn't work. The error output says:

 thread 'main' panicked at 'not yet implemented: Support Box<dyn FnOnce(A, B) -> C>', /Users/edixon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swift-bridge-ir-0.1.52/src/bridged_type.rs:1643:21

It looks like considerable work has been done to support FnOnce, but they're not hooked up in bridged_types. There's just TODOs there. Is there something about the implementation that needs to be fixed? If so I'd be happy to look into it, I'd just appreciate some context :)

Thanks for sharing this project btw, it's amazing 😎

daytime-em avatar Sep 05 '23 03:09 daytime-em

When you declare FnOnce on a Swift type, it doesn't work. The error output says:

Can you comment with a minimal example bridge module that generates this error?

It looks like considerable work has been done to support FnOnce, but they're not hooked up in bridged_types

Right now we support passing an Box<dyn FnOnce> from Rust -> Swift. As in, Rust creates the callback and Swift calls the callback.

Is there something about the implementation that needs to be fixed? If so I'd be happy to look into it

Here are some relevant links that should help with adding support for your Box<dyn FnOnce> case:

https://github.com/chinedufn/swift-bridge/blob/af962ca051ba15b81794575a0837f1e2abde9984/book/src/contributing/adding-support-for-a-signature/README.md

https://github.com/chinedufn/swift-bridge/blob/af962ca051ba15b81794575a0837f1e2abde9984/crates/swift-bridge-ir/src/codegen/codegen_tests/boxed_fnonce_codegen_tests.rs#L382-L430

https://github.com/chinedufn/swift-bridge/blob/af962ca051ba15b81794575a0837f1e2abde9984/crates/swift-integration-tests/src/boxed_functions.rs#L37-L53

Please feel free to ask more questions as you go!

chinedufn avatar Sep 05 '23 11:09 chinedufn

Right now we support passing an Box<dyn FnOnce> from Rust -> Swift. As in, Rust creates the callback and Swift calls the callback.

I think this was the source of my confusion. I was attempting to send a Closure from Swift to Rust, and call it in Rust. I edited the issue title to reflect this.

Thank you for the implementation guidance! If I have more questions can I just tag you here?

daytime-em avatar Sep 05 '23 19:09 daytime-em

If I have more questions can I just tag you here?

Absolutely!

chinedufn avatar Sep 05 '23 20:09 chinedufn