use crate::shared_pointer::kind::SharedPointerKind;
use alloc::boxed::Box;
use alloc::rc::Rc;
use core::fmt;
use core::fmt::Debug;
use core::fmt::Formatter;
use core::mem;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::ops::DerefMut;
use core::ptr;
type UntypedRc = Rc<()>;
pub struct RcK {
inner: ManuallyDrop<UntypedRc>,
}
impl RcK {
#[inline(always)]
fn new_from_inner<T>(rc: Rc<T>) -> RcK {
RcK { inner: ManuallyDrop::new(unsafe { mem::transmute(rc) }) }
}
#[inline(always)]
unsafe fn take_inner<T>(self) -> Rc<T> {
let rc: UntypedRc = ManuallyDrop::into_inner(self.inner);
mem::transmute(rc)
}
#[inline(always)]
unsafe fn as_inner_ref<T>(&self) -> &Rc<T> {
let rc_t: *const Rc<T> =
(self.inner.deref() as *const UntypedRc).cast::<alloc::rc::Rc<T>>();
let _ = mem::transmute::<UntypedRc, Rc<T>>;
&*rc_t
}
#[inline(always)]
unsafe fn as_inner_mut<T>(&mut self) -> &mut Rc<T> {
let rc_t: *mut Rc<T> =
(self.inner.deref_mut() as *mut UntypedRc).cast::<alloc::rc::Rc<T>>();
&mut *rc_t
}
}
unsafe impl SharedPointerKind for RcK {
#[inline(always)]
fn new<T>(v: T) -> RcK {
RcK::new_from_inner(Rc::new(v))
}
#[inline(always)]
fn from_box<T>(v: Box<T>) -> RcK {
RcK::new_from_inner::<T>(Rc::from(v))
}
#[inline(always)]
unsafe fn as_ptr<T>(&self) -> *const T {
Rc::as_ptr(self.as_inner_ref())
}
#[inline(always)]
unsafe fn deref<T>(&self) -> &T {
self.as_inner_ref::<T>().as_ref()
}
#[inline(always)]
unsafe fn try_unwrap<T>(self) -> Result<T, RcK> {
Rc::try_unwrap(self.take_inner()).map_err(RcK::new_from_inner)
}
#[inline(always)]
unsafe fn get_mut<T>(&mut self) -> Option<&mut T> {
Rc::get_mut(self.as_inner_mut())
}
#[inline(always)]
unsafe fn make_mut<T: Clone>(&mut self) -> &mut T {
Rc::make_mut(self.as_inner_mut())
}
#[inline(always)]
unsafe fn strong_count<T>(&self) -> usize {
Rc::strong_count(self.as_inner_ref::<T>())
}
#[inline(always)]
unsafe fn clone<T>(&self) -> RcK {
RcK { inner: ManuallyDrop::new(Rc::clone(self.as_inner_ref())) }
}
#[inline(always)]
unsafe fn drop<T>(&mut self) {
ptr::drop_in_place::<Rc<T>>(self.as_inner_mut());
}
}
impl Debug for RcK {
#[inline(always)]
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
f.write_str("RcK")
}
}
#[cfg(test)]
mod test;