use std::{fmt, ops}; use std::marker::PhantomData; #[repr(C)] pub struct Handle { pointer: *mut u8, marker: PhantomData, } impl Handle { pub fn new(value: T) -> Self { Handle { pointer: Box::into_raw(Box::new(value)) as _, marker: PhantomData, } } pub fn unwrap(self) -> Box { unsafe { Box::from_raw(self.pointer as _) } } } impl Clone for Handle { fn clone(&self) -> Self { Handle { pointer: self.pointer, marker: PhantomData, } } } impl Copy for Handle {} impl ops::Deref for Handle { type Target = T; fn deref(&self) -> &T { unsafe { &*(self.pointer as *mut _) } } } impl ops::DerefMut for Handle { fn deref_mut(&mut self) -> &mut T { unsafe { &mut*(self.pointer as *mut _) } } } impl fmt::Debug for Handle { fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result { //TODO Ok(()) } }