-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #78359 - ssomers:btree_cleanup_mem, r=Mark-Simulacrum
BTreeMap: move generic support functions out of navigate.rs A preparatory step chipped off #78104, useful in general (if at all). r? `@Mark-Simulacrum`
- Loading branch information
Showing
3 changed files
with
42 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use core::intrinsics; | ||
use core::mem; | ||
use core::ptr; | ||
|
||
/// This replaces the value behind the `v` unique reference by calling the | ||
/// relevant function. | ||
/// | ||
/// If a panic occurs in the `change` closure, the entire process will be aborted. | ||
#[inline] | ||
pub fn take_mut<T>(v: &mut T, change: impl FnOnce(T) -> T) { | ||
replace(v, |value| (change(value), ())) | ||
} | ||
|
||
/// This replaces the value behind the `v` unique reference by calling the | ||
/// relevant function, and returns a result obtained along the way. | ||
/// | ||
/// If a panic occurs in the `change` closure, the entire process will be aborted. | ||
#[inline] | ||
pub fn replace<T, R>(v: &mut T, change: impl FnOnce(T) -> (T, R)) -> R { | ||
struct PanicGuard; | ||
impl Drop for PanicGuard { | ||
fn drop(&mut self) { | ||
intrinsics::abort() | ||
} | ||
} | ||
let guard = PanicGuard; | ||
let value = unsafe { ptr::read(v) }; | ||
let (new_value, ret) = change(value); | ||
unsafe { | ||
ptr::write(v, new_value); | ||
} | ||
mem::forget(guard); | ||
ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod borrow; | ||
pub mod map; | ||
mod mem; | ||
mod merge_iter; | ||
mod navigate; | ||
mod node; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters