| 1 | use core::{intrinsics, mem, ptr}; |
| 2 | |
| 3 | /// This replaces the value behind the `v` unique reference by calling the |
| 4 | /// relevant function. |
| 5 | /// |
| 6 | /// If a panic occurs in the `change` closure, the entire process will be aborted. |
| 7 | #[allow (dead_code)] // keep as illustration and for future use |
| 8 | #[inline ] |
| 9 | pub(super) fn take_mut<T>(v: &mut T, change: impl FnOnce(T) -> T) { |
| 10 | replace(v, |value: T| (change(value), ())) |
| 11 | } |
| 12 | |
| 13 | /// This replaces the value behind the `v` unique reference by calling the |
| 14 | /// relevant function, and returns a result obtained along the way. |
| 15 | /// |
| 16 | /// If a panic occurs in the `change` closure, the entire process will be aborted. |
| 17 | #[inline ] |
| 18 | pub(super) fn replace<T, R>(v: &mut T, change: impl FnOnce(T) -> (T, R)) -> R { |
| 19 | struct PanicGuard; |
| 20 | impl Drop for PanicGuard { |
| 21 | fn drop(&mut self) { |
| 22 | intrinsics::abort() |
| 23 | } |
| 24 | } |
| 25 | let guard: PanicGuard = PanicGuard; |
| 26 | let value: T = unsafe { ptr::read(src:v) }; |
| 27 | let (new_value: T, ret: R) = change(value); |
| 28 | unsafe { |
| 29 | ptr::write(dst:v, src:new_value); |
| 30 | } |
| 31 | mem::forget(guard); |
| 32 | ret |
| 33 | } |
| 34 | |