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::pattern::slice::Slice;
callback!(MyCallback(slice: Slice<u8>) -> u8);The generated type definition similar to:
#[repr(transparent)]
pub struct MyCallback(Option<extern "C" fn(Slice<u8>) -> u8>);You can also create the callback from Rust for testing:
use interoptopus::callback;
callback!(MyCallback() -> u8);
let callback = MyCallback::from_fn(|| 42);
assert_eq!(42, callback.call());