use super::*;
use core::ops::{Deref, DerefMut};
#[cfg(feature = "alloc_try_pin_with")]
use core::alloc::AllocError;
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[repr(transparent)]
pub struct UniqueRc<T: ?Sized>(Rc<T>);
impl<T> UniqueRc<T> {
#[cfg(feature = "alloc_pin_with")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_pin_with")))]
#[inline]
pub fn new(data: T) -> Self {
UniqueRc(Rc::new(data))
}
#[cfg(feature = "alloc_try_pin_with")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_try_pin_with")))]
#[inline]
pub fn try_new(data: T) -> Result<Self, AllocError> {
Ok(UniqueRc(Rc::try_new(data)?))
}
#[inline]
pub fn shareable(x: Self) -> Rc<T> {
x.0
}
#[inline]
pub fn shareable_pin(x: Pin<Self>) -> Pin<Rc<T>> {
unsafe { Pin::new_unchecked(Pin::into_inner_unchecked(x).0) }
}
#[inline]
pub fn new_uninit() -> UniqueRc<MaybeUninit<T>> {
UniqueRc::new(MaybeUninit::uninit())
}
}
impl<T> UniqueRc<MaybeUninit<T>> {
#[inline]
pub unsafe fn assume_init(self) -> UniqueRc<T> {
unsafe { core::mem::transmute(self) }
}
}
impl<T> Deref for UniqueRc<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&*self.0
}
}
impl<T> DerefMut for UniqueRc<T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
let ptr = Rc::as_ptr(&self.0) as *mut T;
unsafe { &mut *ptr }
}
}
impl<T> From<UniqueRc<T>> for Pin<UniqueRc<T>> {
fn from(x: UniqueRc<T>) -> Pin<UniqueRc<T>> {
unsafe { Pin::new_unchecked(x) }
}
}
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
#[repr(transparent)]
pub struct UniqueArc<T: ?Sized>(Arc<T>);
impl<T> UniqueArc<T> {
#[cfg(feature = "alloc_pin_with")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_pin_with")))]
#[inline]
pub fn new(data: T) -> Self {
UniqueArc(Arc::new(data))
}
#[cfg(feature = "alloc_try_pin_with")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_try_pin_with")))]
#[inline]
pub fn try_new(data: T) -> Result<Self, AllocError> {
Ok(UniqueArc(Arc::try_new(data)?))
}
#[inline]
pub fn shareable(x: Self) -> Arc<T> {
x.0
}
#[inline]
pub fn shareable_pin(x: Pin<Self>) -> Pin<Arc<T>> {
unsafe { Pin::new_unchecked(Pin::into_inner_unchecked(x).0) }
}
#[inline]
pub fn new_uninit() -> UniqueArc<MaybeUninit<T>> {
UniqueArc::new(MaybeUninit::uninit())
}
}
impl<T> UniqueArc<MaybeUninit<T>> {
#[inline]
pub unsafe fn assume_init(self) -> UniqueArc<T> {
unsafe { core::mem::transmute(self) }
}
}
impl<T> Deref for UniqueArc<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&*self.0
}
}
impl<T> DerefMut for UniqueArc<T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
let ptr = Arc::as_ptr(&self.0) as *mut T;
unsafe { &mut *ptr }
}
}
impl<T> From<UniqueArc<T>> for Pin<UniqueArc<T>> {
fn from(x: UniqueArc<T>) -> Pin<UniqueArc<T>> {
unsafe { Pin::new_unchecked(x) }
}
}