|
1 | 1 | use core::alloc::Allocator; |
| 2 | +#[cfg(not(no_global_oom_handling))] |
| 3 | +use core::convert; |
2 | 4 | use core::marker::PhantomData; |
| 5 | +#[cfg(not(no_global_oom_handling))] |
| 6 | +use core::mem::{self, SizedTypeProperties}; |
| 7 | +#[cfg(not(no_global_oom_handling))] |
| 8 | +use core::ops::{Residual, Try}; |
3 | 9 |
|
4 | 10 | use crate::raw_rc::RefCounter; |
5 | 11 | use crate::raw_rc::raw_rc::RawRc; |
| 12 | +#[cfg(not(no_global_oom_handling))] |
| 13 | +use crate::raw_rc::raw_weak; |
6 | 14 | use crate::raw_rc::raw_weak::RawWeak; |
7 | 15 | use crate::raw_rc::rc_value_pointer::RcValuePointer; |
8 | 16 |
|
|
73 | 81 | } |
74 | 82 | } |
75 | 83 | } |
| 84 | + |
| 85 | +impl<T, A> RawUniqueRc<T, A> { |
| 86 | + #[cfg(not(no_global_oom_handling))] |
| 87 | + pub(super) unsafe fn from_weak_with_value(weak: RawWeak<T, A>, value: T) -> Self { |
| 88 | + unsafe { weak.as_ptr().write(value) }; |
| 89 | + |
| 90 | + Self { weak, _marker: PhantomData, _marker2: PhantomData } |
| 91 | + } |
| 92 | + |
| 93 | + #[cfg(not(no_global_oom_handling))] |
| 94 | + pub(crate) fn new(value: T) -> Self |
| 95 | + where |
| 96 | + A: Allocator + Default, |
| 97 | + { |
| 98 | + unsafe { Self::from_weak_with_value(RawWeak::new_uninit::<0>(), value) } |
| 99 | + } |
| 100 | + |
| 101 | + #[cfg(not(no_global_oom_handling))] |
| 102 | + pub(crate) fn new_in(value: T, alloc: A) -> Self |
| 103 | + where |
| 104 | + A: Allocator, |
| 105 | + { |
| 106 | + unsafe { Self::from_weak_with_value(RawWeak::new_uninit_in::<0>(alloc), value) } |
| 107 | + } |
| 108 | + |
| 109 | + /// Maps the value in a `RawUniqueRc`, reusing the allocation if possible. |
| 110 | + /// |
| 111 | + /// # Safety |
| 112 | + /// |
| 113 | + /// All accesses to `self` must use the same `RefCounter` implementation for `R`. |
| 114 | + #[cfg(not(no_global_oom_handling))] |
| 115 | + pub(crate) unsafe fn map<R, U>(self, f: impl FnOnce(T) -> U) -> RawUniqueRc<U, A> |
| 116 | + where |
| 117 | + A: Allocator, |
| 118 | + R: RefCounter, |
| 119 | + { |
| 120 | + fn wrap_fn<T, U>(f: impl FnOnce(T) -> U) -> impl FnOnce(T) -> Result<U, !> { |
| 121 | + |x| Ok(f(x)) |
| 122 | + } |
| 123 | + |
| 124 | + unsafe { self.try_map::<R, _, _>(wrap_fn(f), convert::identity) }.into_ok() |
| 125 | + } |
| 126 | + |
| 127 | + /// Attempts to map the value in a `RawUniqueRc`, reusing the allocation if possible. |
| 128 | + /// |
| 129 | + /// # Safety |
| 130 | + /// |
| 131 | + /// All accesses to `self` must use the same `RefCounter` implementation for `R`. |
| 132 | + #[cfg(not(no_global_oom_handling))] |
| 133 | + pub(crate) unsafe fn try_map<R, U, V>( |
| 134 | + mut self, |
| 135 | + f: impl FnOnce(T) -> U, |
| 136 | + map_result: impl FnOnce(RawUniqueRc<U::Output, A>) -> V, // How to remove this argument? |
| 137 | + ) -> <U::Residual as Residual<V>>::TryType |
| 138 | + where |
| 139 | + A: Allocator, |
| 140 | + R: RefCounter, |
| 141 | + U: Try, |
| 142 | + U::Residual: Residual<V>, |
| 143 | + { |
| 144 | + // Destruct `self` as a `RawWeak<T, A>` if `f` panics or returns a failure value. |
| 145 | + let guard = unsafe { raw_weak::new_weak_guard::<T, A, R>(&mut self.weak) }; |
| 146 | + |
| 147 | + let (allocation, mapped_value) = if T::LAYOUT == U::Output::LAYOUT |
| 148 | + && R::unique_rc_weak_count(unsafe { R::from_raw_counter(guard.weak_count_unchecked()) }) |
| 149 | + == 1 |
| 150 | + { |
| 151 | + let mapped_value = f(unsafe { guard.as_ptr().read() })?; |
| 152 | + |
| 153 | + // Avoid deallocation on success, reuse the allocation. |
| 154 | + mem::forget(guard); |
| 155 | + |
| 156 | + let allocation = unsafe { self.weak.cast() }; |
| 157 | + |
| 158 | + (allocation, mapped_value) |
| 159 | + } else { |
| 160 | + let value = unsafe { guard.as_ptr().read() }; |
| 161 | + |
| 162 | + drop(guard); |
| 163 | + |
| 164 | + let mapped_value = f(value)?; |
| 165 | + let allocation = RawWeak::new_uninit_in::<0>(self.weak.into_raw_parts().1); |
| 166 | + |
| 167 | + (allocation, mapped_value) |
| 168 | + }; |
| 169 | + |
| 170 | + try { map_result(unsafe { RawUniqueRc::from_weak_with_value(allocation, mapped_value) }) } |
| 171 | + } |
| 172 | +} |
0 commit comments