Macro interoptopus::callback
source · macro_rules! callback { ($name:ident($($param:ident: $ty:ty),*)) => { ... }; ($name:ident($($param:ident: $ty:ty),*) -> $rval:ty $(, namespace = $ns:expr)?) => { ... }; }
Expand description
Defines a callback type, akin to a fn f(T) -> R
wrapped in an Option.
A named delegate will be emitted in languages supporting them, otherwise a regular function pointer. For details, please see the callbacks module.
§Example
This defines a type MyCallback
with a parameter slice
returning an u8
.
use interoptopus::callback;
use interoptopus::patterns::slice::FFISlice;
callback!(MyCallback(slice: FFISlice<u8>) -> u8);
The generated type definition similar to:
#[repr(transparent)]
pub struct MyCallback(Option<extern "C" fn(FFISlice<u8>) -> u8>);
You can also create the callback from Rust for testing:
use interoptopus::callback;
callback!(MyCallback() -> u8);
extern "C" fn my_rust_callback() -> u8 {
42
}
let callback = MyCallback::new(my_rust_callback);
assert_eq!(42, callback.call());