This commit is contained in:
numbers
2023-09-04 06:26:35 -04:00
commit a5fe71ebb3
26 changed files with 1186 additions and 0 deletions

40
src/cffi.rs Normal file
View File

@@ -0,0 +1,40 @@
/// appends zeroes to the end of the string and converts it into a pointer
/// useful for quick ffi
#[macro_export]
macro_rules! cstr {
($str:expr) => {
concat!($str,"\0\0").as_ptr() as *const i8
}
}
/// 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``` |
#[macro_export]
macro_rules! cfn {
( ($($t:ty),*)) => {
extern "C" fn($( $t ),* )
};
( ($($t:ty),*) -> $r:ty) => {
extern "C" fn($( $t ),* ) -> $r
}
}
/// utility macro for pointer chains
#[macro_export]
macro_rules! ptr_chain {
($x: expr, $y:expr) => {{
(*core::mem::transmute::<_,*const usize>(core::mem::transmute::<_,usize>($x) + $y))
}};
($x: expr, $y:expr, $( $z:expr ),+ ) => {{
$crate::ptr_chain!( $crate::ptr_chain!($x, $y), $( $z ),+ )
}};
}