|
| 1 | +use core::alloc::Allocator; |
| 2 | +use core::fmt::{self, Debug, Display, Formatter, Pointer}; |
| 3 | +use core::hash::{Hash, Hasher}; |
| 4 | +use core::marker::{PhantomData, Unsize}; |
| 5 | +use core::ops::{CoerceUnsized, DispatchFromDyn}; |
| 6 | +use core::ptr::NonNull; |
| 7 | + |
| 8 | +use crate::alloc::Global; |
| 9 | +use crate::raw_rc::RcOps; |
| 10 | +use crate::raw_rc::raw_rc::RawRc; |
| 11 | +use crate::raw_rc::raw_weak::RawWeak; |
| 12 | + |
| 13 | +/// A uniquely owned `RawRc` that allows multiple weak references but only one strong reference. |
| 14 | +/// `RawUniqueRc` does not implement `Drop`, user should call `RawUniqueRc::drop` manually to drop |
| 15 | +/// this object. |
| 16 | +#[repr(transparent)] |
| 17 | +pub(crate) struct RawUniqueRc<T, A> |
| 18 | +where |
| 19 | + T: ?Sized, |
| 20 | +{ |
| 21 | + // A `RawUniqueRc` is just a `RawWeak` that has zero strong count but with the value |
| 22 | + // initialized. |
| 23 | + weak: RawWeak<T, A>, |
| 24 | + |
| 25 | + // Defines the ownership of `T` for drop-check. |
| 26 | + _marker: PhantomData<T>, |
| 27 | + |
| 28 | + // Invariance is necessary for soundness: once other `RawWeak` references exist, we already have |
| 29 | + // a form of shared mutability! |
| 30 | + _marker2: PhantomData<*mut T>, |
| 31 | +} |
| 32 | + |
| 33 | +impl<T, A> RawUniqueRc<T, A> |
| 34 | +where |
| 35 | + T: ?Sized, |
| 36 | +{ |
| 37 | + pub(crate) unsafe fn downgrade<R>(&self) -> RawWeak<T, A> |
| 38 | + where |
| 39 | + A: Clone, |
| 40 | + R: RcOps, |
| 41 | + { |
| 42 | + unsafe { self.weak.clone::<R>() } |
| 43 | + } |
| 44 | + |
| 45 | + pub(crate) unsafe fn drop<R>(&mut self) |
| 46 | + where |
| 47 | + A: Allocator, |
| 48 | + R: RcOps, |
| 49 | + { |
| 50 | + unsafe { self.weak.assume_init_drop::<R>() }; |
| 51 | + } |
| 52 | + |
| 53 | + pub(crate) unsafe fn into_rc<R>(self) -> RawRc<T, A> |
| 54 | + where |
| 55 | + R: RcOps, |
| 56 | + { |
| 57 | + unsafe fn inner<R>(value_ptr: NonNull<()>) |
| 58 | + where |
| 59 | + R: RcOps, |
| 60 | + { |
| 61 | + unsafe { |
| 62 | + R::unlock_strong_count(super::strong_count_ptr_from_value_ptr(value_ptr).as_ref()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + unsafe { |
| 67 | + inner::<R>(self.weak.as_ptr().cast()); |
| 68 | + |
| 69 | + RawRc::from_weak(self.weak) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<T, A> RawUniqueRc<T, A> { |
| 75 | + #[cfg(not(no_global_oom_handling))] |
| 76 | + pub(super) unsafe fn from_weak_with_value(weak: RawWeak<T, A>, value: T) -> Self { |
| 77 | + unsafe { weak.as_ptr().write(value) }; |
| 78 | + |
| 79 | + Self { weak, _marker: PhantomData, _marker2: PhantomData } |
| 80 | + } |
| 81 | + |
| 82 | + #[cfg(not(no_global_oom_handling))] |
| 83 | + pub(crate) fn new(value: T) -> Self |
| 84 | + where |
| 85 | + A: Allocator + Default, |
| 86 | + { |
| 87 | + unsafe { Self::from_weak_with_value(RawWeak::new_uninit::<0>(), value) } |
| 88 | + } |
| 89 | + |
| 90 | + #[cfg(not(no_global_oom_handling))] |
| 91 | + pub(crate) fn new_in(value: T, alloc: A) -> Self |
| 92 | + where |
| 93 | + A: Allocator, |
| 94 | + { |
| 95 | + unsafe { Self::from_weak_with_value(RawWeak::new_uninit_in::<0>(alloc), value) } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<T, A> AsMut<T> for RawUniqueRc<T, A> |
| 100 | +where |
| 101 | + T: ?Sized, |
| 102 | +{ |
| 103 | + fn as_mut(&mut self) -> &mut T { |
| 104 | + unsafe { self.weak.as_ptr().as_mut() } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<T, A> AsRef<T> for RawUniqueRc<T, A> |
| 109 | +where |
| 110 | + T: ?Sized, |
| 111 | +{ |
| 112 | + fn as_ref(&self) -> &T { |
| 113 | + unsafe { self.weak.as_ptr().as_ref() } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<T, U, A> CoerceUnsized<RawUniqueRc<U, A>> for RawUniqueRc<T, A> |
| 118 | +where |
| 119 | + T: Unsize<U> + ?Sized, |
| 120 | + U: ?Sized, |
| 121 | + A: Allocator, |
| 122 | +{ |
| 123 | +} |
| 124 | + |
| 125 | +impl<T, A> Debug for RawUniqueRc<T, A> |
| 126 | +where |
| 127 | + T: Debug + ?Sized, |
| 128 | +{ |
| 129 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 130 | + <T as Debug>::fmt(self.as_ref(), f) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl<T, U> DispatchFromDyn<RawUniqueRc<U, Global>> for RawUniqueRc<T, Global> |
| 135 | +where |
| 136 | + T: Unsize<U> + ?Sized, |
| 137 | + U: ?Sized, |
| 138 | +{ |
| 139 | +} |
| 140 | + |
| 141 | +impl<T, A> Display for RawUniqueRc<T, A> |
| 142 | +where |
| 143 | + T: Display + ?Sized, |
| 144 | +{ |
| 145 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 146 | + <T as Display>::fmt(self.as_ref(), f) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +impl<T, A> Eq for RawUniqueRc<T, A> where T: Eq + ?Sized {} |
| 151 | + |
| 152 | +impl<T, A> Hash for RawUniqueRc<T, A> |
| 153 | +where |
| 154 | + T: Hash + ?Sized, |
| 155 | +{ |
| 156 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 157 | + T::hash(self.as_ref(), state); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +impl<T, A> Ord for RawUniqueRc<T, A> |
| 162 | +where |
| 163 | + T: Ord + ?Sized, |
| 164 | +{ |
| 165 | + fn cmp(&self, other: &Self) -> core::cmp::Ordering { |
| 166 | + T::cmp(self.as_ref(), other.as_ref()) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +impl<T, A> PartialEq for RawUniqueRc<T, A> |
| 171 | +where |
| 172 | + T: PartialEq + ?Sized, |
| 173 | +{ |
| 174 | + fn eq(&self, other: &Self) -> bool { |
| 175 | + T::eq(self.as_ref(), other.as_ref()) |
| 176 | + } |
| 177 | + |
| 178 | + fn ne(&self, other: &Self) -> bool { |
| 179 | + T::ne(self.as_ref(), other.as_ref()) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +impl<T, A> PartialOrd for RawUniqueRc<T, A> |
| 184 | +where |
| 185 | + T: PartialOrd + ?Sized, |
| 186 | +{ |
| 187 | + fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { |
| 188 | + T::partial_cmp(self.as_ref(), other.as_ref()) |
| 189 | + } |
| 190 | + |
| 191 | + fn lt(&self, other: &Self) -> bool { |
| 192 | + T::lt(self.as_ref(), other.as_ref()) |
| 193 | + } |
| 194 | + |
| 195 | + fn le(&self, other: &Self) -> bool { |
| 196 | + T::le(self.as_ref(), other.as_ref()) |
| 197 | + } |
| 198 | + |
| 199 | + fn gt(&self, other: &Self) -> bool { |
| 200 | + T::gt(self.as_ref(), other.as_ref()) |
| 201 | + } |
| 202 | + |
| 203 | + fn ge(&self, other: &Self) -> bool { |
| 204 | + T::ge(self.as_ref(), other.as_ref()) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +impl<T, A> Pointer for RawUniqueRc<T, A> |
| 209 | +where |
| 210 | + T: ?Sized, |
| 211 | +{ |
| 212 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 213 | + <&T as Pointer>::fmt(&self.as_ref(), f) |
| 214 | + } |
| 215 | +} |
0 commit comments