Reputation: 107
I'm looking at a C API written with Rust and are using callback functions. The implementation pattern looks like this
type FooCallback = extern "C" fn(data: &FooData, context: FooContext);
trait FooCallbackExt {
fn call(&self, data: FooData, context: FooContext);
}
impl FooCallbackExt for FooCallback {
fn call(&self, data_in: FooData, context: FooContext) {
// Doing cool things
}
}
I can test this from the C-side by creating a callback function like this:
void callback(const FooData* data, FooContext context)
Now I would like to test this interface from the Rust-side as well, as close as possible to the exposed C interface, to isolate an issue that I believe is happening in Rust.
The Rust function taking this data looks like this:
fn foo(&mut self, callback: FooCallback, context: FooContext)
but I'm confused about how how to create a callback from the Rust side.
Do I need to create some type of converter or layer in-between? Or can I create a Rust type-version of the callback but have them both use the same trait?
Upvotes: 0
Views: 56
Reputation: 59962
You can easily create a callback function like that in Rust like so:
extern "C" fn bar(data: &FooData, context: FooContext) {
// whatever
}
Upvotes: 2