I’m writing a plugin (shared library) for an application that exposes a C api and I’m using rust + bindgen to do this.
The basic operation of this plugin is that I have to expose a unmangled extern “C” function named specifically after the name of the shared library, and in that I do some registration with the API to setup my plugin. This all works well so far: https://github.com/x37v/puredata-rust/blob/wrapper/examples/helloworld-orig/src/lib.rs
Knowing that I’m working with an unsafe API, but with knowledge of the guarantees of that API as far as thread safety etc etc, my question is this:
Is it okay to pass
MyStruct::struct_method(&mut self) to a C API in place of
unsafe extern "C" fn(*mut MyStruct) for callback registration where
I am able to do this in practice and it works well so far, though I have to
transmute the function pointer when passing to my
bindgen generated bindings.
I just want to make sure I’m not shooting myself in the foot before I go much further down this path. I would really like to not have to write a bunch of
unsafe extern "C' fn functions that simply call back into my struct methods if possible.
It does look like I should
extern "C" my methods so that I can be sure of the calling convention…
BTW, you may notice that I actually have to transmute to a function that takes no arguments… though, the callback actually does take arguments, that is just an artifact of how the API works.
Thanks for any input you can provide!