mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-09 06:47:59 -06:00
Module object
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user