Last active
April 24, 2019 17:45
-
-
Save Slabity/8ee8c5c41036f9440058faba3fe981a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// map_ptr! macro takes an &Option<&mut &mut [T]> and returns the raw pointer | |
// map_len! macro takes an &Option<&mut &mut [T]> and returns the length | |
// map_shrink! macro takes an &Option<&mut &mut [T]> and dynamically shrinks it | |
// This function will fill the passed in buffers with data from an FFI function. | |
pub fn load_buffers(buffer_a: Option<&mut &mut [u32]>, buffer_b: Option<&mut &mut [u64]>) { | |
let mut ffi_buffer_holder = Buffer { | |
ptr_a: map_ptr!(&buffer_a), | |
ptr_b: map_ptr!(&buffer_b), | |
len_a: map_len!(&buffer_a), | |
len_b: map_len!(&buffer_b) | |
}; | |
// This function will fill the buffers that the `ptr_*` fields point to. | |
// Only fills it up to the length of their respective `len_*` fields. | |
unsafe { | |
call_ffi_function(&mut ffi_buffer_holder); | |
} | |
map_shrink!(buffer_a, ffi_buffer_holder.len_a); | |
map_shrink!(buffer_b, ffi_buffer_holder.len_b); | |
} | |
// This function also fills a buffer, but the type is not known until after the FFI function is called. | |
pub fn load_nasty_buffer(buffer: Option<&mut &mut [???]>) -> u32 { | |
let mut ffi_nasty_buffer_holder = NastyBuffer { | |
flags: 0, | |
ptr: map_ptr!(&buffer), | |
len: map_len!(&buffer), | |
}; | |
// This function fills the buffer with data. | |
// The type of data can be one of 2 different sized types, only known after we can read the flags | |
unsafe { | |
call_nasty_ffi_function(&mut ffi_nasty_buffer_holder); | |
} | |
map_shrink!(buffer, ffi_buffer_holder.len); | |
// Return the flags value so the user knows what type this is. | |
ffi_nasty_buffer_holder.flags | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment