1use base::alloc::{dealloc, Layout};
4use base::borrow;
5use base::cell::Cell;
6use base::cmp::Ordering;
7use base::convert::{From, AsMut};
8use base::fmt;
9use base::hash::{Hash, Hasher};
10use base::marker::{PhantomData, Unpin};
11use base::mem;
12use base::ops::{Deref, DerefMut};
13use base::ptr::{self, NonNull};
14
15use base::borrow::BorrowMut;
16
17use base::prelude::v1::*;
18
19use smart_pointer::{SmartPointer, IntoMut, SmartPointerMut};
20
21use crate::ReferenceCounted;
22
23pub struct Rc<T: ?Sized> {
25 ptr: NonNull<RcBox<T>>,
26 phantom: PhantomData<RcBox<T>>,
27}
28
29struct RcBox<T: ?Sized> {
30 strong: Cell<usize>,
31 data: T,
32}
33
34impl<T: ?Sized> Rc<T> {
35 fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
36 Self { ptr, phantom: PhantomData }
37 }
38
39 #[inline]
40 fn inner(&self) -> &RcBox<T> {
41 unsafe { self.ptr.as_ref() }
44 }
45
46 fn ptr(&self) -> *mut RcBox<T> {
47 self.ptr.as_ptr()
48 }
49
50 fn ref_count(&self) -> usize {
51 self.inner().strong.get()
52 }
53
54 #[inline]
55 fn inc_strong(&self) {
56 let strong = self.ref_count();
57
58 if strong == 0 || strong == usize::MAX {
63 panic!();
64 }
65 self.inner().strong.set(strong + 1);
66 }
67
68 #[inline]
69 fn dec_strong(&self) {
70 self.inner().strong.set(self.ref_count() - 1);
71 }
72}
73
74impl<T: ?Sized> Clone for Rc<T> {
75 #[inline]
79 fn clone(&self) -> Rc<T> {
80 self.inc_strong();
81 Self::from_inner(self.ptr)
82 }
83}
84
85impl<T: ?Sized> Drop for Rc<T> {
86 #[inline]
110 fn drop(&mut self) {
111 unsafe {
112 self.dec_strong();
113 if self.ref_count() == 0 {
114 ptr::drop_in_place(self.ptr.as_mut());
116
117 dealloc(self.ptr().cast(), Layout::for_value(self.ptr.as_ref()));
118 }
119 }
120 }
121}
122
123impl<T: ?Sized> Deref for Rc<T> {
124 type Target = T;
125
126 #[inline]
127 fn deref(&self) -> &T {
128 &self.inner().data
129 }
130}
131
132impl<T: ?Sized> borrow::Borrow<T> for Rc<T> {
133 fn borrow(&self) -> &T {
134 &**self
135 }
136}
137
138impl<T: ?Sized> AsRef<T> for Rc<T> {
139 fn as_ref(&self) -> &T {
140 &**self
141 }
142}
143
144impl<T: ?Sized + fmt::Display> fmt::Display for Rc<T> {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 fmt::Display::fmt(&**self, f)
147 }
148}
149
150impl<T: ?Sized + fmt::Debug> fmt::Debug for Rc<T> {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 fmt::Debug::fmt(&**self, f)
153 }
154}
155
156impl<T: ?Sized> fmt::Pointer for Rc<T> {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 fmt::Pointer::fmt(&(&**self as *const T), f)
159 }
160}
161
162impl<T: ?Sized> SmartPointer<T> for Rc<T> {
163 fn new(data: T) -> Rc<T> where T: Sized {
164 Self::from_inner(
165 Box::leak(Box::new(RcBox { strong: Cell::new(1), data })).into(),
166 )
167 }
168
169 fn try_unwrap(this: Self) -> Result<T, Self> where T: Sized {
170 if Rc::ref_count(&this) == 1 {
171 unsafe {
172 let val = ptr::read(&*this); dealloc(this.ptr().cast(), Layout::for_value(this.ptr.as_ref()));
174 mem::forget(this);
175 Ok(val)
176 }
177 } else {
178 Err(this)
179 }
180 }
181}
182
183pub struct UniqueRc<T: ?Sized>(Rc<T>);
184
185unsafe impl<T: ?Sized + Sync + Send> Send for UniqueRc<T> {}
186unsafe impl<T: ?Sized + Sync + Send> Sync for UniqueRc<T> {}
187
188impl<T: ?Sized> Deref for UniqueRc<T> {
189 type Target = T;
190
191 #[inline]
192 fn deref(&self) -> &T {
193 self.0.deref()
194 }
195}
196
197impl<T: ?Sized> borrow::Borrow<T> for UniqueRc<T> {
198 fn borrow(&self) -> &T {
199 self.0.borrow()
200 }
201}
202
203impl<T: ?Sized> AsRef<T> for UniqueRc<T> {
204 fn as_ref(&self) -> &T {
205 self.0.as_ref()
206 }
207}
208
209impl<T: ?Sized + fmt::Display> fmt::Display for UniqueRc<T> {
210 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211 self.0.fmt(f)
212 }
213}
214
215impl<T: ?Sized + fmt::Debug> fmt::Debug for UniqueRc<T> {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 self.0.fmt(f)
218 }
219}
220
221impl<T: ?Sized> fmt::Pointer for UniqueRc<T> {
222 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
223 self.0.fmt(f)
224 }
225}
226
227impl<T: ?Sized> SmartPointer<T> for UniqueRc<T> {
228 fn new(data: T) -> Self where T: Sized {
229 UniqueRc(Rc::new(data))
230 }
231
232 fn try_unwrap(this: Self) -> Result<T, Self> where T: Sized {
233 let this = this.0;
234
235 unsafe {
236 let elem = ptr::read(&this.ptr.as_ref().data);
237 dealloc(this.ptr().cast(), Layout::for_value(this.ptr.as_ref()));
238 mem::forget(this);
239 Ok(elem)
240 }
241 }
242}
243
244
245impl<T: ?Sized> DerefMut for UniqueRc<T> {
246 fn deref_mut(&mut self) -> &mut T {
247 unsafe { &mut (*self.0.ptr()).data }
249 }
250}
251
252impl<T: ?Sized> BorrowMut<T> for UniqueRc<T> {
253 fn borrow_mut(&mut self) -> &mut T {
254 &mut **self
255 }
256}
257
258impl<T: ?Sized> AsMut<T> for UniqueRc<T> {
259 fn as_mut(&mut self) -> &mut T {
260 &mut **self
261 }
262}
263
264impl<T: ?Sized> SmartPointerMut<T> for UniqueRc<T> {}
265
266impl<T: ?Sized> Into<Rc<T>> for UniqueRc<T> {
267 fn into(self) -> Rc<T> {
268 self.0
269 }
270}
271
272impl<T: ?Sized> IntoMut<T> for Rc<T> {
273 type MutablePointer = UniqueRc<T>;
274
275 fn can_make_mut(this: &Self) -> bool {
276 this.ref_count() == 1
277 }
278
279 unsafe fn into_mut_unchecked(this: Self) -> Self::MutablePointer {
280 UniqueRc(this)
281 }
282
283 unsafe fn get_mut_unchecked(this: &Self) -> &mut T {
288 unsafe { &mut (*this.ptr.as_ptr()).data }
291 }
292}
293
294impl<T: ?Sized> ReferenceCounted<T> for Rc<T> {
295 fn reference_count(this: &Self) -> usize {
296 this.ref_count()
297 }
298}
299
300impl<T: Default> Default for Rc<T> {
301 fn default() -> Rc<T> {
312 Rc::new(Default::default())
313 }
314}
315
316impl<T: Default> Default for UniqueRc<T> {
317 fn default() -> UniqueRc<T> {
319 UniqueRc::new(Default::default())
320 }
321}
322
323impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
324 #[inline]
330 fn eq(&self, other: &Rc<T>) -> bool {
331 (**self).eq(&**other)
332 }
333
334 #[inline]
339 fn ne(&self, other: &Rc<T>) -> bool {
340 (**self).ne(&**other)
341 }
342}
343
344impl<T: ?Sized + Eq> Eq for Rc<T> {}
345
346impl<T: ?Sized + PartialEq> PartialEq for UniqueRc<T> {
347 #[inline]
353 fn eq(&self, other: &UniqueRc<T>) -> bool {
354 (**self).eq(&**other)
355 }
356
357 #[inline]
362 fn ne(&self, other: &UniqueRc<T>) -> bool {
363 (**self).ne(&**other)
364 }
365}
366
367impl<T: ?Sized + Eq> Eq for UniqueRc<T> {}
368
369impl<T: ?Sized + PartialOrd> PartialOrd for Rc<T> {
370 fn partial_cmp(&self, other: &Rc<T>) -> Option<Ordering> {
374 (**self).partial_cmp(&**other)
375 }
376
377 fn lt(&self, other: &Rc<T>) -> bool {
381 *(*self) < *(*other)
382 }
383
384 fn le(&self, other: &Rc<T>) -> bool {
388 *(*self) <= *(*other)
389 }
390
391 fn gt(&self, other: &Rc<T>) -> bool {
395 *(*self) > *(*other)
396 }
397
398 fn ge(&self, other: &Rc<T>) -> bool {
402 *(*self) >= *(*other)
403 }
404}
405
406impl<T: ?Sized + PartialOrd> PartialOrd for UniqueRc<T> {
407 fn partial_cmp(&self, other: &UniqueRc<T>) -> Option<Ordering> {
411 (**self).partial_cmp(&**other)
412 }
413
414 fn lt(&self, other: &UniqueRc<T>) -> bool {
418 *(*self) < *(*other)
419 }
420
421 fn le(&self, other: &UniqueRc<T>) -> bool {
425 *(*self) <= *(*other)
426 }
427
428 fn gt(&self, other: &UniqueRc<T>) -> bool {
432 *(*self) > *(*other)
433 }
434
435 fn ge(&self, other: &UniqueRc<T>) -> bool {
439 *(*self) >= *(*other)
440 }
441}
442
443impl<T: ?Sized + Ord> Ord for Rc<T> {
444 fn cmp(&self, other: &Rc<T>) -> Ordering {
448 (**self).cmp(&**other)
449 }
450}
451
452impl<T: ?Sized + Ord> Ord for UniqueRc<T> {
453 fn cmp(&self, other: &UniqueRc<T>) -> Ordering {
457 (**self).cmp(&**other)
458 }
459}
460
461impl<T> From<T> for Rc<T> {
462 fn from(t: T) -> Self {
463 Rc::new(t)
464 }
465}
466
467impl<T> From<T> for UniqueRc<T> {
468 fn from(t: T) -> Self {
469 UniqueRc::new(t)
470 }
471}
472
473impl<T: ?Sized + Hash> Hash for Rc<T> {
474 fn hash<H: Hasher>(&self, state: &mut H) {
475 (**self).hash(state)
476 }
477}
478
479impl<T: ?Sized + Hash> Hash for UniqueRc<T> {
480 fn hash<H: Hasher>(&self, state: &mut H) {
481 (**self).hash(state)
482 }
483}
484
485impl<T: ?Sized> Unpin for Rc<T> {}
486
487impl<T: ?Sized> Unpin for UniqueRc<T> {}