use crate::alloc::Box; use core::marker::PhantomData; use core::ptr::NonNull; #[repr(transparent)] pub struct Own where T: ?Sized, { pub ptr: NonNull, } unsafe impl Send for Own where T: ?Sized {} unsafe impl Sync for Own where T: ?Sized {} impl Copy for Own where T: ?Sized {} impl Clone for Own where T: ?Sized, { fn clone(&self) -> Self { *self } } impl Own where T: ?Sized, { pub fn new(ptr: Box) -> Self { Own { ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) }, } } pub fn cast(self) -> Own { Own { ptr: self.ptr.cast(), } } pub unsafe fn boxed(self) -> Box { Box::from_raw(self.ptr.as_ptr()) } pub fn by_ref(&self) -> Ref { Ref { ptr: self.ptr, lifetime: PhantomData, } } pub fn by_mut(&mut self) -> Mut { Mut { ptr: self.ptr, lifetime: PhantomData, } } } #[repr(transparent)] pub struct Ref<'a, T> where T: ?Sized, { pub ptr: NonNull, lifetime: PhantomData<&'a T>, } impl<'a, T> Copy for Ref<'a, T> where T: ?Sized {} impl<'a, T> Clone for Ref<'a, T> where T: ?Sized, { fn clone(&self) -> Self { *self } } impl<'a, T> Ref<'a, T> where T: ?Sized, { pub fn new(ptr: &'a T) -> Self { Ref { ptr: NonNull::from(ptr), lifetime: PhantomData, } } #[cfg(not(anyhow_no_ptr_addr_of))] pub fn from_raw(ptr: NonNull) -> Self { Ref { ptr, lifetime: PhantomData, } } pub fn cast(self) -> Ref<'a, U::Target> { Ref { ptr: self.ptr.cast(), lifetime: PhantomData, } } #[cfg(not(anyhow_no_ptr_addr_of))] pub fn by_mut(self) -> Mut<'a, T> { Mut { ptr: self.ptr, lifetime: PhantomData, } } #[cfg(not(anyhow_no_ptr_addr_of))] pub fn as_ptr(self) -> *const T { self.ptr.as_ptr() as *const T } pub unsafe fn deref(self) -> &'a T { &*self.ptr.as_ptr() } } #[repr(transparent)] pub struct Mut<'a, T> where T: ?Sized, { pub ptr: NonNull, lifetime: PhantomData<&'a mut T>, } impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {} impl<'a, T> Clone for Mut<'a, T> where T: ?Sized, { fn clone(&self) -> Self { *self } } impl<'a, T> Mut<'a, T> where T: ?Sized, { #[cfg(anyhow_no_ptr_addr_of)] pub fn new(ptr: &'a mut T) -> Self { Mut { ptr: NonNull::from(ptr), lifetime: PhantomData, } } pub fn cast(self) -> Mut<'a, U::Target> { Mut { ptr: self.ptr.cast(), lifetime: PhantomData, } } #[cfg(not(anyhow_no_ptr_addr_of))] pub fn by_ref(self) -> Ref<'a, T> { Ref { ptr: self.ptr, lifetime: PhantomData, } } pub fn extend<'b>(self) -> Mut<'b, T> { Mut { ptr: self.ptr, lifetime: PhantomData, } } pub unsafe fn deref_mut(self) -> &'a mut T { &mut *self.ptr.as_ptr() } } impl<'a, T> Mut<'a, T> { pub unsafe fn read(self) -> T { self.ptr.as_ptr().read() } } // Force turbofish on all calls of `.cast::()`. pub trait CastTo { type Target; } impl CastTo for T { type Target = T; }