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

43 lines
1.4 KiB
Rust

use crate::dynamic_data::{EventBatchId, EventData};
use crate::ffi::ffi_handle::FFIHandle;
use std::sync::Arc;
/// Wrapper class for easier use of an external function pointer.
#[repr(C)]
pub(super) struct NativeEventHook {
/// The actual C function to be called. Note that we pass the batch id as a pair of u64s. This
/// is because u128 does not have a stable ABI yet.
f: extern "C" fn(FFIHandle<Arc<EventData>>, u64, u64),
}
impl NativeEventHook {
/// Calls the actual wrapped function in the correct format.
fn call_self(&self, b: Arc<EventData>, batch_id: EventBatchId) {
let batch_id = batch_id.as_u64_pair();
(self.f)(FFIHandle::get_handle(b.into()), batch_id.0, batch_id.1);
}
}
/// A tuple with the arguments of the event hook function
type EventHookArgs<'a, 'b, 'c> = (Arc<EventData>, EventBatchId);
impl FnMut<EventHookArgs<'_, '_, '_>> for NativeEventHook {
extern "rust-call" fn call_mut(&mut self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0, args.1)
}
}
impl FnOnce<EventHookArgs<'_, '_, '_>> for NativeEventHook {
type Output = ();
extern "rust-call" fn call_once(self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0, args.1)
}
}
impl Fn<EventHookArgs<'_, '_, '_>> for NativeEventHook {
extern "rust-call" fn call(&self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0, args.1)
}
}