macro_rules! event_subscribe {
( unsafe $event:expr , $ty:ty , $callback:expr $(,)? ) => { ... };
( unsafe $event:expr => $ty:ty , $callback:expr $(,)? ) => { ... };
( $event:expr , $ty:ty , $callback:expr $(,)? ) => { ... };
( $event:expr => $ty:ty , $callback:expr $(,)? ) => { ... };
}Expand description
Macro to subscribe to an event with a wrapped callback.
This macro is unsafe.
See event_subscribe_typed for more information.
Returns a Revertible to revert the subscribe.
§Usage
unsafe {
event_subscribe!("MY_EVENT" => i32, |data| {
use nexus::log::{log, LogLevel};
log(LogLevel::Info, "My Addon", format!("Received MY_EVENT with {data:?}"));
})
}.revert_on_unload();The event identifier may be dynamic and the callback can be a function name.
let event: &str = "MY_EVENT";
fn event_callback(data: Option<&i32>) {
use nexus::log::{log, LogLevel};
log(LogLevel::Info, "My Addon", format!("Received MY_EVENT with {data:?}"));
}
let revertible = unsafe { event_subscribe!(event => i32, event_callback) };
revertible.revert();The unsafe keyword can be moved into the macro call:
event_subscribe!(unsafe "MY_EVENT" => (), event_callback);Note that the payload type corresponds to the pointee in Nexus documentation. If you are interested in the pointer itself, you have to cast the obtained reference back to a pointer:
use std::ffi::{c_char, CStr};
event_subscribe!(unsafe "EV_ACCOUNT_NAME" => c_char, |data| {
if let Some(data) = data {
let ptr = data as *const c_char;
let c_str = unsafe { CStr::from_ptr(ptr) };
}
});