1use super::*;
2use core::ops::{Deref, DerefMut};
3#[cfg(feature = "alloc_try_pin_with")]
4use core::alloc::AllocError;
5
6#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
12#[repr(transparent)]
13pub struct UniqueRc<T: ?Sized>(Rc<T>);
14
15impl<T> UniqueRc<T> {
16 #[cfg(feature = "alloc_pin_with")]
18 #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_pin_with")))]
19 #[inline]
20 pub fn new(data: T) -> Self {
21 UniqueRc(Rc::new(data))
22 }
23
24 #[cfg(feature = "alloc_try_pin_with")]
26 #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_try_pin_with")))]
27 #[inline]
28 pub fn try_new(data: T) -> Result<Self, AllocError> {
29 Ok(UniqueRc(Rc::try_new(data)?))
30 }
31
32 #[inline]
34 pub fn shareable(x: Self) -> Rc<T> {
35 x.0
36 }
37
38 #[inline]
40 pub fn shareable_pin(x: Pin<Self>) -> Pin<Rc<T>> {
41 unsafe { Pin::new_unchecked(Pin::into_inner_unchecked(x).0) }
42 }
43
44 #[inline]
46 pub fn new_uninit() -> UniqueRc<MaybeUninit<T>> {
47 UniqueRc::new(MaybeUninit::uninit())
48 }
49}
50
51impl<T> UniqueRc<MaybeUninit<T>> {
52 #[inline]
58 pub unsafe fn assume_init(self) -> UniqueRc<T> {
59 unsafe { core::mem::transmute(self) }
60 }
61}
62
63impl<T> Deref for UniqueRc<T> {
64 type Target = T;
65 #[inline]
66 fn deref(&self) -> &T {
67 &*self.0
68 }
69}
70
71impl<T> DerefMut for UniqueRc<T> {
72 #[inline]
73 fn deref_mut(&mut self) -> &mut T {
74 let ptr = Rc::as_ptr(&self.0) as *mut T;
75 unsafe { &mut *ptr }
77 }
78}
79
80impl<T> From<UniqueRc<T>> for Pin<UniqueRc<T>> {
81 fn from(x: UniqueRc<T>) -> Pin<UniqueRc<T>> {
82 unsafe { Pin::new_unchecked(x) }
84 }
85}
86
87#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
93#[repr(transparent)]
94pub struct UniqueArc<T: ?Sized>(Arc<T>);
95
96impl<T> UniqueArc<T> {
97 #[cfg(feature = "alloc_pin_with")]
99 #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_pin_with")))]
100 #[inline]
101 pub fn new(data: T) -> Self {
102 UniqueArc(Arc::new(data))
103 }
104
105 #[cfg(feature = "alloc_try_pin_with")]
107 #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc_try_pin_with")))]
108 #[inline]
109 pub fn try_new(data: T) -> Result<Self, AllocError> {
110 Ok(UniqueArc(Arc::try_new(data)?))
111 }
112
113 #[inline]
115 pub fn shareable(x: Self) -> Arc<T> {
116 x.0
117 }
118
119 #[inline]
121 pub fn shareable_pin(x: Pin<Self>) -> Pin<Arc<T>> {
122 unsafe { Pin::new_unchecked(Pin::into_inner_unchecked(x).0) }
123 }
124
125 #[inline]
127 pub fn new_uninit() -> UniqueArc<MaybeUninit<T>> {
128 UniqueArc::new(MaybeUninit::uninit())
129 }
130}
131
132impl<T> UniqueArc<MaybeUninit<T>> {
133 #[inline]
139 pub unsafe fn assume_init(self) -> UniqueArc<T> {
140 unsafe { core::mem::transmute(self) }
141 }
142}
143
144impl<T> Deref for UniqueArc<T> {
145 type Target = T;
146 #[inline]
147 fn deref(&self) -> &T {
148 &*self.0
149 }
150}
151
152impl<T> DerefMut for UniqueArc<T> {
153 #[inline]
154 fn deref_mut(&mut self) -> &mut T {
155 let ptr = Arc::as_ptr(&self.0) as *mut T;
156 unsafe { &mut *ptr }
158 }
159}
160
161impl<T> From<UniqueArc<T>> for Pin<UniqueArc<T>> {
162 fn from(x: UniqueArc<T>) -> Pin<UniqueArc<T>> {
163 unsafe { Pin::new_unchecked(x) }
165 }
166}