use core::{marker, mem, ptr}; use core::ops::{Deref, DerefMut}; pub trait Dealloc { unsafe fn dealloc(p: *mut T); } #[repr(transparent)] pub struct BoxLike> { inner: ptr::NonNull, _phantom: marker::PhantomData, _dropper: marker::PhantomData, } impl> BoxLike { #[inline] pub unsafe fn from_raw_unchecked(p: *mut T) -> Self { BoxLike { inner: ptr::NonNull::new_unchecked(p), _phantom: marker::PhantomData, _dropper: marker::PhantomData, } } #[inline] pub unsafe fn from_raw(p: *mut T) -> Option { if p.is_null() { None } else { Some(Self::from_raw_unchecked(p)) } } #[inline] pub fn into_raw(self) -> *mut T { let p = self.inner.as_ptr(); mem::forget(self); p } } impl> Drop for BoxLike { #[inline] fn drop(&mut self) { let p = self.inner.as_ptr(); unsafe { ptr::drop_in_place(p); D::dealloc(p); } } } impl> Deref for BoxLike { type Target = T; #[inline] fn deref(&self) -> &T { unsafe { self.inner.as_ref() } } } impl> DerefMut for BoxLike { #[inline] fn deref_mut(&mut self) -> &mut T { unsafe { self.inner.as_mut() } } } pub struct DeallocFree; impl Dealloc for DeallocFree { #[inline] unsafe fn dealloc(p: *mut T) { libc::free(p as _); } } pub type CBox = BoxLike;