Module object

This commit is contained in:
Michael Mikovsky
2025-11-05 22:59:01 -07:00
parent f601c3a58e
commit ad6e6ffef2
5 changed files with 77 additions and 96 deletions
+1 -41
View File
@@ -1,31 +1,3 @@
#[macro_export]
macro_rules! module {
($module_name:ident { $(fn $fn_name:ident($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?);* $(;)? }) => {
#[allow(non_camel_case_types)]
pub struct $module_name;
impl $module_name {
$(
#[inline(always)]
pub fn $fn_name(&self, $($arg: $ty),*) $(-> $ret)? {
$fn_name($($arg),*)
}
)*
/// Create a new instance of this module
pub fn new() -> Self {
Self
}
}
impl Default for $module_name {
fn default() -> Self {
Self::new()
}
}
};
}
#[macro_export]
macro_rules! module_interface {
($interface_name:ident { $(fn $fn_name:ident($($arg:ident : $ty:ty),* $(,)?) $(-> $ret:ty)?);* $(;)? }) => {
@@ -39,25 +11,13 @@ macro_rules! module_interface {
}
impl $interface_name {
/// Unsafe cast from a module type to this interface
///
/// # Safety
///
/// The caller must ensure that:
/// - The module has exactly the same function signatures in the same order
/// - The functions follow the C calling convention
/// - The module's memory layout matches this interface
pub unsafe fn from_module<T>(module: &T) -> Self {
*(module as *const T as *const Self)
}
/// Create from raw function pointers
///
/// # Safety
///
/// The caller must ensure all function pointers are valid and have
/// the correct signatures
pub unsafe fn from_raw(
pub fn from_raw(
$($fn_name: extern "C" fn($($ty),*) $(-> $ret)?),*
) -> Self {
Self {