ralfbiedert / interoptopus

The polyglot bindings generator for your library (C#, C, Python, …) 🐙
MIT License
321 stars 28 forks source link

Added support for extracting the fn from a macro callback #98

Closed Earthmark closed 8 months ago

Earthmark commented 8 months ago

This allows wrappers to be easily (and generically) be implemented for callbacks.

callback!(MyCallback(value: u32) -> u32);

fn wrap_callback<Arg, Ret, Callback>(wrapper: Callback, arg: Arg) -> Ret
where
    Callback: Into<Option<extern "C" fn(Arg) -> Ret>>,
    Ret: Default,
{
    // log that it was called or something.
    if let Some(callback) = wrapper.into() {
        callback(arg)
    } else {
        Default::default()
    }
}

#[ffi_function]
#[no_mangle]
pub extern "C" fn pattern_callback_1(callback: MyCallback, x: u32) -> u32 {
    wrap_callback(callback, x)
}
ralfbiedert commented 8 months ago

Thanks!