Giter Site home page Giter Site logo

Comments (3)

chinedufn avatar chinedufn commented on June 2, 2024 1

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

Absolutely!

from swift-bridge.

chinedufn avatar chinedufn commented on June 2, 2024

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!

from swift-bridge.

daytime-em avatar daytime-em commented on June 2, 2024

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?

from swift-bridge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.