1 use std::{ 2 fmt::Debug, 3 ops::{Deref, DerefMut}, 4 }; 5 6 pub mod logset; 7 pub mod mm; 8 9 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] 10 pub struct ObjectWrapper<T> { 11 object: Box<T>, 12 } 13 14 impl<T: Debug + Sized> ObjectWrapper<T> { 15 pub fn new(buf: &[u8]) -> Option<Self> { 16 if buf.len() != std::mem::size_of::<T>() { 17 println!( 18 "ObjectWrapper::new(): buf.len() '{}' != std::mem::size_of::<T>(): '{}'", 19 buf.len(), 20 std::mem::size_of::<T>() 21 ); 22 return None; 23 } 24 let x = unsafe { std::ptr::read(buf.as_ptr() as *const T) }; 25 26 let object = Box::new(x); 27 28 // let object = ManuallyDrop::new(x); 29 Some(Self { object }) 30 } 31 } 32 33 impl<T> DerefMut for ObjectWrapper<T> { 34 fn deref_mut(&mut self) -> &mut Self::Target { 35 &mut self.object 36 } 37 } 38 39 impl<T> Deref for ObjectWrapper<T> { 40 type Target = T; 41 42 fn deref(&self) -> &Self::Target { 43 &self.object 44 } 45 } 46