This commit is contained in:
Intege-rs
2024-11-13 21:41:26 -05:00
commit 03066e2e55
29 changed files with 1549 additions and 0 deletions

38
sub/core/src/ffi.rs Normal file
View File

@@ -0,0 +1,38 @@
/// appends zeroes to the end of the string and converts it into a pointer
/// useful for quick ffi
pub macro cstr($str:expr) {
concat!($str,"\0\0").as_ptr() as *const _
}
/// utility macro for inline c functions
///
/// | macro | rust |
/// |-----------|----------|
/// | ```cfn!( () -> usize )``` | ``` extern "C" fn() -> usize``` |
/// | ```cfn!( (usize) -> usize )``` | ``` extern "C" fn(usize) -> usize``` |
/// | ```cfn!( (usize) )``` | ``` extern "C" fn(usize)``` |
/// | ```cfn!( (u32, usize, usize) -> u32 )``` | ``` extern "C" fn(u32, usize, usize) -> u32``` |
pub macro cfn($str:expr) {
( ($($t:ty),*)) => {
extern "C" fn($( $t ),* )
};
( ($($t:ty),*) -> $r:ty) => {
extern "C" fn($( $t ),* ) -> $r
}
}
#[inline(always)]
pub fn _chain_helper<const N: usize>(address: usize, links: [usize;N]) -> usize {
links.iter().fold(address, |b,r| unsafe {*((b + *r) as *const usize)} )
}
#[allow(unused)]
use crate::cast_traits::As;
pub macro ptr_chain( $base:expr, $($link:expr),* ) {
_chain_helper(As::<usize>::cast($base), [
$(As::<usize>::cast($link)),*
])
}