PkmnLib_rs/src/ffi/dynamic_data/libraries/misc_library.rs

47 lines
1.4 KiB
Rust

use crate::dynamic_data::{Gen7MiscLibrary, MiscLibrary};
use crate::ffi::ffi_handle::FFIHandle;
use crate::static_data::TimeOfDay;
use std::sync::Arc;
/// Instantiates a new MiscLibrary.
#[no_mangle]
extern "C" fn gen_7_misc_library_new(get_time_of_day_fn: FFIGetTimeOfDayFn) -> FFIHandle<Arc<dyn MiscLibrary>> {
let v: Arc<dyn MiscLibrary> = Arc::new(Gen7MiscLibrary::new(Box::new(get_time_of_day_fn)));
FFIHandle::get_handle(v.into())
}
/// Wrapper class for easier use of an external function pointer.
#[repr(C)]
pub(super) struct FFIGetTimeOfDayFn {
/// 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() -> u8,
}
impl FFIGetTimeOfDayFn {
/// Calls the actual wrapped function in the correct format.
fn call_self(&self) -> TimeOfDay {
unsafe { std::mem::transmute((self.f)()) }
}
}
impl FnMut<()> for FFIGetTimeOfDayFn {
extern "rust-call" fn call_mut(&mut self, _: ()) -> Self::Output {
self.call_self()
}
}
impl FnOnce<()> for FFIGetTimeOfDayFn {
type Output = TimeOfDay;
extern "rust-call" fn call_once(self, _: ()) -> Self::Output {
self.call_self()
}
}
impl Fn<()> for FFIGetTimeOfDayFn {
extern "rust-call" fn call(&self, _: ()) -> Self::Output {
self.call_self()
}
}