PkmnLib_rs/src/ffi/mod.rs

165 lines
5.0 KiB
Rust
Raw Normal View History

2022-10-14 14:53:30 +00:00
/// The foreign function interfaces for the dynamic data
2022-10-14 08:33:19 +00:00
mod dynamic_data;
2023-06-24 13:05:58 +00:00
/// The handling of handles for the foreign function interfaces
mod ffi_handle;
2022-10-14 14:53:30 +00:00
/// The foreign function interfaces for that static data
2022-09-18 16:02:13 +00:00
mod static_data;
2023-09-24 16:24:02 +00:00
use ffi_handle::*;
2022-10-14 14:53:30 +00:00
/// Helper type for clearer functions.
#[repr(transparent)]
#[derive(Debug, Copy, Clone)]
struct OwnedPtrString(*mut c_char);
2022-10-14 14:53:30 +00:00
/// Helper type for clearer functions.
type NonOwnedPtrString = *const c_char;
impl Default for OwnedPtrString {
fn default() -> Self {
OwnedPtrString(std::ptr::null_mut())
}
}
2023-01-03 12:56:20 +00:00
/// Generates a basic getter foreign function interface.
macro_rules! ffi_handle_arc_dyn_getter {
2023-01-03 12:56:20 +00:00
(
$type:ty, $func:ident, $returns: ty
) => {
paste::paste! {
#[no_mangle]
extern "C" fn [< $type:snake _ $func >](ptr: FFIHandle<Arc<dyn $type>>) -> $returns {
ptr.from_ffi_handle().$func()
2023-01-03 12:56:20 +00:00
}
}
};
}
2022-10-14 14:53:30 +00:00
/// Generates a basic getter foreign function interface where the return type is a [`crate::StringKey`].
macro_rules! ffi_handle_arc_stringkey_getter {
2022-09-18 16:02:13 +00:00
(
$type:ty, $func:ident
) => {
paste::paste! {
#[no_mangle]
extern "C" fn [< $type:lower _ $func >](ptr: FFIHandle<Arc<dyn $type>>) -> NonOwnedPtrString {
std::ffi::CString::new(ptr.from_ffi_handle().$func().str()).unwrap().into_raw()
2022-09-18 16:02:13 +00:00
}
}
};
}
2022-10-14 14:53:30 +00:00
/// Generates a foreign function interface for a vec. This generates a length function, and a getter.
macro_rules! ffi_handle_vec_value_getters {
2022-09-18 16:02:13 +00:00
(
2023-01-03 12:56:20 +00:00
$name:ident, $type:ty, $func:ident, $returns: ty
2022-09-18 16:02:13 +00:00
) => {
paste::paste! {
#[no_mangle]
extern "C" fn [< $name:lower _ $func _length>](ptr: FFIHandle<Arc<$type>>) -> usize {
ptr.from_ffi_handle().$func().len()
2022-09-18 16:02:13 +00:00
}
#[no_mangle]
extern "C" fn [< $name:lower _ $func _get>](ptr: FFIHandle<Arc<$type>>, index: usize) -> $returns {
*ptr.from_ffi_handle().$func().get(index).unwrap()
2022-09-18 16:02:13 +00:00
}
}
};
}
2022-10-14 14:53:30 +00:00
/// Generates a foreign function interface for a vec of [`crate::StringKey`]. This generates a
/// length function, and a getter.
macro_rules! ffi_handle_vec_stringkey_getters {
2022-09-18 16:02:13 +00:00
(
$type:ty, $func:ident
2022-09-18 16:02:13 +00:00
) => {
paste::paste! {
#[no_mangle]
extern "C" fn [< $type:lower _ $func _length>](ptr: FFIHandle<Arc<dyn $type>>) -> usize {
ptr.from_ffi_handle().$func().len()
2022-09-18 16:02:13 +00:00
}
#[no_mangle]
extern "C" fn [< $type:lower _ $func _get>](ptr: FFIHandle<Arc<dyn $type>>, index: usize) -> OwnedPtrString {
OwnedPtrString(CString::new(ptr.from_ffi_handle().$func().get(index).unwrap().str()).unwrap().into_raw())
2022-09-18 16:02:13 +00:00
}
}
};
}
2023-09-24 16:24:02 +00:00
use ffi_handle_arc_dyn_getter;
use ffi_handle_arc_stringkey_getter;
use ffi_handle_vec_stringkey_getters;
use ffi_handle_vec_value_getters;
2023-04-15 12:34:42 +00:00
use std::ffi::{c_char, CString};
2022-09-18 16:02:13 +00:00
2022-10-14 14:53:30 +00:00
/// Helper utility class to wrap a pointer for extern functions.
2022-09-18 16:02:13 +00:00
#[repr(C)]
2023-09-24 16:24:02 +00:00
struct ExternPointer<T: ?Sized> {
2022-10-14 14:53:30 +00:00
/// The wrapped pointer.
2022-09-18 16:02:13 +00:00
ptr: *mut T,
}
impl<T: ?Sized> ExternPointer<T> {
2022-10-14 14:53:30 +00:00
/// Get the internal pointer as reference.
#[allow(clippy::panic)] // We currently allow this as these should never be null, but we might want to change this in the future.
2022-09-18 16:02:13 +00:00
pub(self) fn as_ref(&self) -> &T {
unsafe {
2022-10-14 14:53:30 +00:00
self.ptr
.as_ref()
.unwrap_or_else(|| panic!("Given pointer of type '{}' was null", std::any::type_name::<T>()))
2022-09-18 16:02:13 +00:00
}
}
}
2023-04-15 12:34:42 +00:00
/// The result of a FFI call that can either be an error or a value.
#[repr(C)]
#[repr(packed)]
pub struct FFIResult<T: Copy + Default> {
/// If the result is ok, this is null, otherwise this is a pointer to the error string.
err: NonOwnedPtrString,
2023-04-15 12:34:42 +00:00
/// The value or error.
value: T,
2023-04-15 12:34:42 +00:00
}
impl<T: Copy + Default> FFIResult<T> {
2023-04-15 12:34:42 +00:00
/// Creates a new NativeResult with the given value.
pub fn ok(value: T) -> Self {
Self {
err: std::ptr::null(),
value,
2023-04-15 12:34:42 +00:00
}
}
/// Creates a new NativeResult with the given error.
#[allow(clippy::unwrap_used)] // We know for certain this is not empty.
pub fn err(err: anyhow_ext::Error) -> Self {
2023-04-15 12:34:42 +00:00
Self {
err: CString::new(err.to_string()).unwrap().into_raw(),
value: Default::default(),
2023-04-15 12:34:42 +00:00
}
}
/// Creates a new NativeResult with the given error string.
#[allow(clippy::unwrap_used)] // We know for certain this is not empty.
pub fn err_from_str(err: &str) -> Self {
Self {
err: CString::new(err).unwrap().into_raw(),
value: Default::default(),
}
}
2023-04-15 12:34:42 +00:00
}
impl<T: Copy + Default> From<anyhow::Result<T>> for FFIResult<T> {
2023-04-15 12:34:42 +00:00
fn from(value: anyhow::Result<T>) -> Self {
match value {
Ok(v) => Self::ok(v),
Err(e) => Self::err(e),
}
}
}
impl<T: Copy + Default> From<T> for FFIResult<T> {
2023-04-15 12:34:42 +00:00
fn from(value: T) -> Self {
Self::ok(value)
}
}