use core::mem;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::ptr;
use super::Arc;
#[derive(Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct ArcBorrow<'a, T: ?Sized + 'a>(pub(crate) &'a T);
impl<'a, T> Copy for ArcBorrow<'a, T> {}
impl<'a, T> Clone for ArcBorrow<'a, T> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<'a, T> ArcBorrow<'a, T> {
#[inline]
pub fn clone_arc(&self) -> Arc<T> {
let arc = unsafe { Arc::from_raw(self.0) };
mem::forget(arc.clone());
arc
}
#[inline]
pub unsafe fn from_ref(r: &'a T) -> Self {
ArcBorrow(r)
}
#[inline]
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
ptr::eq(this.0 as *const T, other.0 as *const T)
}
#[inline]
pub fn with_arc<F, U>(&self, f: F) -> U
where
F: FnOnce(&Arc<T>) -> U,
T: 'static,
{
let transient = unsafe { ManuallyDrop::new(Arc::from_raw(self.0)) };
f(&transient)
}
#[inline]
pub fn get(&self) -> &'a T {
self.0
}
}
impl<'a, T> Deref for ArcBorrow<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.0
}
}
#[cfg(feature = "unsize")]
unsafe impl<'lt, T: 'lt, U: ?Sized + 'lt> unsize::CoerciblePtr<U> for ArcBorrow<'lt, T> {
type Pointee = T;
type Output = ArcBorrow<'lt, U>;
fn as_sized_ptr(&mut self) -> *mut T {
self.0 as *const T as *mut T
}
unsafe fn replace_ptr(self, new: *mut U) -> ArcBorrow<'lt, U> {
let inner = ManuallyDrop::new(self);
ArcBorrow(inner.0.replace_ptr(new))
}
}