use core::{marker::PhantomData, ptr::NonNull};
pub(super) struct DormantMutRef<'a, T> {
ptr: NonNull<T>,
_marker: PhantomData<&'a mut T>,
}
unsafe impl<'a, T> Send for DormantMutRef<'a, T> where &'a mut T: Send {}
unsafe impl<'a, T> Sync for DormantMutRef<'a, T> where &'a mut T: Sync {}
impl<'a, T> DormantMutRef<'a, T> {
pub fn new(val: &'a mut T) -> (&'a mut T, DormantMutRef<'a, T>) {
let mut ptr = NonNull::from(val);
let new_val = unsafe { ptr.as_mut() };
(
new_val,
DormantMutRef {
ptr,
_marker: PhantomData,
},
)
}
pub unsafe fn awaken(mut self) -> &'a mut T {
unsafe { self.ptr.as_mut() }
}
pub unsafe fn reborrow(&mut self) -> &'a mut T {
unsafe { self.ptr.as_mut() }
}
}
pub(super) struct DestroyableRef<'a, T> {
ptr: NonNull<T>,
_marker: PhantomData<&'a T>,
}
unsafe impl<'a, T> Send for DestroyableRef<'a, T> where &'a T: Send {}
unsafe impl<'a, T> Sync for DestroyableRef<'a, T> where &'a T: Sync {}
impl<'a, T> DestroyableRef<'a, T> {
pub fn new(val: &'a T) -> Self {
DestroyableRef {
ptr: NonNull::from(val),
_marker: PhantomData,
}
}
pub fn borrow(&self) -> &'a T {
unsafe { self.ptr.as_ref() }
}
}
pub(super) struct DestroyableMutRef<'a, T> {
ptr: NonNull<T>,
_marker: PhantomData<&'a mut T>,
}
unsafe impl<'a, T> Send for DestroyableMutRef<'a, T> where &'a mut T: Send {}
unsafe impl<'a, T> Sync for DestroyableMutRef<'a, T> where &'a mut T: Sync {}
impl<'a, T> DestroyableMutRef<'a, T> {
pub fn new(val: &'a mut T) -> Self {
DestroyableMutRef {
ptr: NonNull::from(val),
_marker: PhantomData,
}
}
pub fn borrow(&self) -> &T {
unsafe { self.ptr.as_ref() }
}
pub fn borrow_mut(&mut self) -> &mut T {
unsafe { self.ptr.as_mut() }
}
pub fn into(mut self) -> &'a mut T {
unsafe { self.ptr.as_mut() }
}
}