2019-09-10 19:55:32 +00:00
|
|
|
/// A wrapper around Nix's Source class that provides the Read trait.
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct Source {
|
|
|
|
fun: extern "C" fn(this: *mut libc::c_void, data: &mut [u8]) -> usize,
|
|
|
|
this: *mut libc::c_void,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::io::Read for Source {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> {
|
|
|
|
let n = (self.fun)(self.this, buf);
|
|
|
|
assert!(n <= buf.len());
|
|
|
|
Ok(n)
|
|
|
|
}
|
|
|
|
}
|
2019-09-15 21:09:30 +00:00
|
|
|
|
|
|
|
pub struct CBox<T> {
|
|
|
|
pub ptr: *mut libc::c_void,
|
|
|
|
phantom: std::marker::PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CBox<T> {
|
|
|
|
pub fn new(t: T) -> Self {
|
|
|
|
unsafe {
|
|
|
|
let size = std::mem::size_of::<T>();
|
|
|
|
let ptr = libc::malloc(size);
|
|
|
|
*(ptr as *mut T) = t; // FIXME: probably UB
|
|
|
|
Self {
|
|
|
|
ptr,
|
|
|
|
phantom: std::marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|