Files
x2/sub/core/src/ffi.rs
Intege-rs 03066e2e55 v0.0.0b
2024-11-13 21:41:26 -05:00

38 lines
1.1 KiB
Rust

/// 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)),*
])
}