cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Support passing function pointers across the FFI

Open dtolnay opened this issue 4 years ago • 5 comments

i.e. function parameters with the type fn(...) -> ....

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        fn f(arg: u8, callback: fn(u8));
    }
}

fn f(arg: u8, callback: fn(u8)) {
    callback(256 - arg);
}

dtolnay avatar Mar 02 '20 01:03 dtolnay

Passing function pointers from Rust to C++ was implemented in #79 + #85. It remains to implement passing the reverse direction.

dtolnay avatar Apr 13 '20 23:04 dtolnay

@dtolnay What's the plan for solving this? You are going to use std::function? Can I help with the implementation?

hanusek avatar Nov 09 '21 16:11 hanusek

@dtolnay What's the plan for solving this? You are going to use std::function? Can I help with the implementation?

Do you have any progress about this?

I want this feature, and as cxx not support it, so I have to use

#[no_mangle]
extern "C" fn register_callback(extern "C" fn(c_int)) {
  todo!()
}

to register function pointer(with specific function signature) on a static memory, and in Rust call the callback

Can I do something for support this feature? @dtolnay and this design is ok?

wendajiang avatar Apr 24 '22 06:04 wendajiang

Here's a pattern to use std::function with current cxx facilities.

adetaylor avatar Nov 14 '22 12:11 adetaylor

Here's a pattern to use std::function with current cxx facilities.

I used this, but it does not support dynamic libraries, can you give me a hand, tks!!

Undefined symbols for architecture arm64: "_cn$rongcloud$cxxbridge1$MyFunction$call", referenced from: imlib::ffi::MyFunction::call::hb153764149e230b4 in imlib.4dg0tx688zy81d57.rcgu.o

https://github.com/ios-yifan/rust-std.git

@adetaylor

ios-yifan avatar May 17 '23 11:05 ios-yifan