use crate::Brc;
use core::ffi::c_void;
use core::fmt::{Debug, Formatter};
use core::mem::ManuallyDrop;
use core::ptr::NonNull;
#[repr(transparent)]
pub struct BrcK {
value_ptr: NonNull<c_void>,
}
impl BrcK {
#[inline]
fn from_brc<T>(brc: Brc<T>) -> Self {
let brc = ManuallyDrop::new(brc);
BrcK {
value_ptr: unsafe { NonNull::new_unchecked(Brc::as_ptr(&brc).cast_mut()).cast() },
}
}
#[inline]
unsafe fn into_brc<T>(self) -> Brc<T> {
unsafe { Brc::from_raw(self.value_ptr.as_ptr().cast::<T>()) }
}
#[inline]
unsafe fn as_brc_mut<T>(&mut self) -> &mut Brc<T> {
unsafe { &mut *core::ptr::from_mut::<Self>(self).cast::<Brc<T>>() }
}
#[inline]
unsafe fn as_brc<T>(&self) -> &Brc<T> {
unsafe { &*core::ptr::from_ref::<Self>(self).cast::<Brc<T>>() }
}
}
unsafe impl archery::SharedPointerKind for BrcK {
#[inline]
fn new<T>(v: T) -> Self {
Self::from_brc(Brc::new(v))
}
#[inline]
fn from_box<T>(v: Box<T>) -> Self {
Self::from_brc::<T>(Brc::from(v))
}
#[inline]
unsafe fn as_ptr<T>(&self) -> *const T {
self.value_ptr.as_ptr().cast()
}
#[inline]
unsafe fn deref<T>(&self) -> &T {
unsafe { self.value_ptr.cast::<T>().as_ref() }
}
#[inline]
unsafe fn try_unwrap<T>(self) -> Result<T, Self> {
unsafe { Brc::try_unwrap(Self::into_brc(self)).map_err(Self::from_brc) }
}
#[inline]
unsafe fn get_mut<T>(&mut self) -> Option<&mut T> {
Brc::get_mut(unsafe { self.as_brc_mut() })
}
#[inline]
unsafe fn make_mut<T: Clone>(&mut self) -> &mut T {
Brc::make_mut(unsafe { self.as_brc_mut() })
}
unsafe fn strong_count<T>(&self) -> usize {
unimplemented!("A Brc cannot always give an accurate strong count")
}
#[inline]
unsafe fn clone<T>(&self) -> Self {
Self::from_brc(unsafe { Brc::clone(self.as_brc::<T>()) })
}
#[inline]
unsafe fn drop<T>(&mut self) {
unsafe { core::ptr::drop_in_place::<Brc<T>>(self.as_brc_mut()) }
}
}
impl Debug for BrcK {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BrcK").finish_non_exhaustive()
}
}