v0.0.0b
This commit is contained in:
38
sub/core/src/ffi.rs
Normal file
38
sub/core/src/ffi.rs
Normal 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)),*
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user