Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Box<FnOnce> in extern "Rust" #242

Open
daytime-em opened this issue Sep 5, 2023 · 3 comments
Open

Support Box<FnOnce> in extern "Rust" #242

daytime-em opened this issue Sep 5, 2023 · 3 comments

Comments

@daytime-em
Copy link

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 daytime-em changed the title Support for FnOnce not enabled Support for Box<FnOnce> not enabled Sep 5, 2023
@chinedufn
Copy link
Owner

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

/// Verify that we can pass a callback with an opaque Rust arg from Rust to Swift.
mod test_swift_takes_callback_return_opaque_rust_type {
use super::*;
fn bridge_module_tokens() -> TokenStream {
quote! {
mod ffi {
extern "Swift" {
fn some_function(callback: Box<dyn FnOnce() -> ARustType>);
}
extern "Rust" {
type ARustType;
}
}
}
}
fn expected_rust_tokens() -> ExpectedRustTokens {
ExpectedRustTokens::ContainsMany(vec![
quote! {
pub fn some_function (callback: Box<dyn FnOnce() -> super::ARustType>) {
unsafe {
__swift_bridge__some_function(
Box::into_raw(Box::new(callback)) as *mut Box<dyn FnOnce() -> super::ARustType>
)
}
}
},
quote! {
#[export_name = "__swift_bridge__$some_function$param0"]
pub extern "C" fn some_function_param0(some_function_callback: *mut Box<dyn FnOnce() -> super::ARustType>) -> *mut super::ARustType {
Box::into_raw(Box::new({
let val: super::ARustType = unsafe { Box::from_raw(some_function_callback)() };
val
})) as *mut super::ARustType
}
#[export_name = "__swift_bridge__$some_function$_free$param0"]
pub extern "C" fn free_some_function_param0(some_function_callback: *mut Box<dyn FnOnce() -> super::ARustType>) {
let _ = unsafe { Box::from_raw(some_function_callback) };
}
},
quote! {
#[link_name = "__swift_bridge__$some_function"]
fn __swift_bridge__some_function(callback: *mut Box<dyn FnOnce() -> super::ARustType>);
},
])
}

// TODO
// extern "Rust" {
// fn rust_takes_callback_fnonce_no_args_no_return(arg: Box<dyn FnOnce() -> ()>);
// fn rust_takes_callback_fnonce_primitive(doubling_fn: Box<dyn FnOnce(u8) -> u8>);
// fn rust_takes_callback_fnonce_opaque_rust(
// doubling_fn: Box<dyn FnOnce(CallbackTestOpaqueRustType) -> CallbackTestOpaqueRustType>,
// );
//
// fn rust_takes_callback_fnonce_two_params(
// arg: Box<dyn FnOnce(i16, CallbackTestOpaqueRustType)>,
// );
//
// fn rust_takes_two_callbacks_fnonce_noop(
// arg1: Box<dyn FnOnce()>,
// arg2: Box<dyn FnOnce() -> ()>,
// );
// }

Please feel free to ask more questions as you go!

@daytime-em daytime-em changed the title Support for Box<FnOnce> not enabled Support Box<FnOnce> in extern "Rust" Sep 5, 2023
@daytime-em
Copy link
Author

Right now we support passing an Box 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?

@chinedufn
Copy link
Owner

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

Absolutely!

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

No branches or pull requests

2 participants