PkmnLib_rs/src/ffi/dynamic_data/models/native_event_hook.rs

39 lines
1.1 KiB
Rust

use crate::dynamic_data::Event;
/// Wrapper class for easier use of an external function pointer.
#[repr(C)]
pub(super) struct NativeEventHook {
/// The actual C function to be called.
f: extern "C" fn(*const Event<'_>),
}
impl NativeEventHook {
/// Calls the actual wrapped function in the correct format.
fn call_self(&self, b: &&Event<'_>) {
(self.f)(*b as *const Event)
}
}
/// A tuple with the arguments of the event hook function
type EventHookArgs<'a, 'b, 'c> = (&'a Box<&'b Event<'c>>,);
impl FnMut<EventHookArgs<'_, '_, '_>> for NativeEventHook {
extern "rust-call" fn call_mut(&mut self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0)
}
}
impl FnOnce<EventHookArgs<'_, '_, '_>> for NativeEventHook {
type Output = ();
extern "rust-call" fn call_once(self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0)
}
}
impl Fn<EventHookArgs<'_, '_, '_>> for NativeEventHook {
extern "rust-call" fn call(&self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0)
}
}