use core::marker::PhantomData;
use core::mem;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::panic::{RefUnwindSafe, UnwindSafe};
use core::ptr::NonNull;
use super::Arc;
#[derive(Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct ArcBorrow<'a, T: ?Sized + 'a>(pub(crate) NonNull<T>, pub(crate) PhantomData<&'a T>);
unsafe impl<'a, T: ?Sized + Sync + Send> Send for ArcBorrow<'a, T> {}
unsafe impl<'a, T: ?Sized + Sync + Send> Sync for ArcBorrow<'a, T> {}
impl<'a, T: RefUnwindSafe + ?Sized> UnwindSafe for ArcBorrow<'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.as_ptr()) };
mem::forget(arc.clone());
arc
}
#[inline]
pub unsafe fn from_ptr(ptr: *const T) -> Self {
unsafe { ArcBorrow(NonNull::new_unchecked(ptr as *mut T), PhantomData) }
}
#[inline]
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
this.0 == other.0
}
#[inline]
pub fn strong_count(this: &Self) -> usize {
Self::with_arc(this, |arc| Arc::strong_count(arc))
}
#[inline]
pub fn with_arc<F, U>(&self, f: F) -> U
where
F: FnOnce(&Arc<T>) -> U,
{
let transient = unsafe { ManuallyDrop::new(Arc::from_raw(self.0.as_ptr())) };
f(&transient)
}
#[inline]
pub fn get(&self) -> &'a T {
unsafe { &*self.0.as_ptr() }
}
}
impl<'a, T> Deref for ArcBorrow<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.get()
}
}
#[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_ptr()
}
unsafe fn replace_ptr(self, new: *mut U) -> ArcBorrow<'lt, U> {
let inner = ManuallyDrop::new(self);
ArcBorrow(inner.0.replace_ptr(new), PhantomData)
}
}
#[test]
fn clone_arc_borrow() {
let x = Arc::new(42);
let b: ArcBorrow<'_, i32> = x.borrow_arc();
let y = b.clone_arc();
assert_eq!(x, y);
}