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 passing function pointers across the FFI #52

Open
dtolnay opened this issue Mar 2, 2020 · 5 comments · May be fixed by #1100
Open

Support passing function pointers across the FFI #52

dtolnay opened this issue Mar 2, 2020 · 5 comments · May be fixed by #1100

Comments

@dtolnay
Copy link
Owner

dtolnay commented Mar 2, 2020

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
Copy link
Owner Author

dtolnay commented Apr 13, 2020

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

@hanusek
Copy link

hanusek commented Nov 9, 2021

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

@wendajiang
Copy link

@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?

@paandahl paandahl linked a pull request Sep 21, 2022 that will close this issue
@adetaylor
Copy link
Collaborator

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

@ios-yifan
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants