new helper map_struct and map_struct_mut

This commit is contained in:
Dietmar Maurer 2018-12-27 09:20:17 +01:00
parent a198d74fc0
commit dc3de618ed
1 changed files with 15 additions and 0 deletions

View File

@ -13,6 +13,21 @@ use std::os::unix::io::AsRawFd;
pub mod timer; pub mod timer;
fn map_struct<T>(buffer: &[u8]) -> Result<&T, Error> {
if buffer.len() < ::std::mem::size_of::<T>() {
bail!("unable to map struct - buffer too small");
}
return Ok(unsafe { & * (buffer.as_ptr() as *const T) });
}
fn map_struct_mut<T>(buffer: &mut [u8]) -> Result<&mut T, Error> {
if buffer.len() < ::std::mem::size_of::<T>() {
bail!("unable to map struct - buffer too small");
}
return Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) });
}
pub fn file_set_contents<P: AsRef<Path>>( pub fn file_set_contents<P: AsRef<Path>>(
path: P, path: P,
data: &[u8], data: &[u8],