I've had a look over the rust foreign function interface, and successfully (and happily) can call a c library from my rust code.
However, I can't seem to find any details on how to register a callback to invoke rust code, from within the c-code scope.
Is this even possible?
As a justification for 'why would you even do that?'; specifically I'm looking at embedding either lua or python in a rust application, and exposing a scripting-api for scripts that run on the embedded runtime.
Invoking these would be something along the lines of:
All of these steps except for the ones in bold I've managed to get working, and I've done some trivial work using a scheduler, which the C-callbacks dump 'run me' commands into a queue and when control returns to the rust scope, the application queries the queue and runs the commands in it...
...but it's a little awkward from the scripting side, because it means multiple nested async callbacks, and the point of the scripting layer is to simplify the code that needs to go into the scripting layer.
Yes, you can pass a callback function from Rust to C and invoke it there. The main thing to know is that you have to define the function with the extern "C" attribute.
Untested example (no Rust compiler here):
Rust side:
extern "C" fn callback(a:i32) {
println!("I'm called from C with value {0}", a);
}
#[link(name = "mylib")]
extern {
fn register_callback(cb: extern "C" fn(i32)) -> i32;
}
fn main() {
unsafe {
register_callback(callback);
}
...
}
C side:
typedef void (*rust_callback)(int32_t);
rust_callback cb;
int32_t register_callback(rust_callback callback) {
cb = callback;
return 1;
}
void thread() {
// do sth
cb(xyz);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With