mirror of
https://git.intege.rs/xlib/x.git
synced 2025-12-05 20:35:01 +00:00
initial
This commit is contained in:
40
src/cffi.rs
Normal file
40
src/cffi.rs
Normal 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 ),+ )
|
||||
}};
|
||||
}
|
||||
Reference in New Issue
Block a user