From dc3de618edc343f0ceb74df9db55492e12624f23 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 27 Dec 2018 09:20:17 +0100 Subject: [PATCH] new helper map_struct and map_struct_mut --- src/tools.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/tools.rs b/src/tools.rs index 30a8b190..6c22a62c 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -13,6 +13,21 @@ use std::os::unix::io::AsRawFd; pub mod timer; +fn map_struct(buffer: &[u8]) -> Result<&T, Error> { + if buffer.len() < ::std::mem::size_of::() { + bail!("unable to map struct - buffer too small"); + } + return Ok(unsafe { & * (buffer.as_ptr() as *const T) }); +} + +fn map_struct_mut(buffer: &mut [u8]) -> Result<&mut T, Error> { + if buffer.len() < ::std::mem::size_of::() { + bail!("unable to map struct - buffer too small"); + } + return Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) }); +} + + pub fn file_set_contents>( path: P, data: &[u8],