Skip to content

Commit 0da7941

Browse files
committed
BTreeMap: move generic functions out of navigate.rs
1 parent 1cd97ca commit 0da7941

File tree

3 files changed

+42
-40
lines changed

3 files changed

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

library/alloc/src/collections/btree/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod borrow;
22
pub mod map;
3+
mod mem;
34
mod merge_iter;
45
mod navigate;
56
mod node;

library/alloc/src/collections/btree/navigate.rs

+7-40
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use core::borrow::Borrow;
22
use core::cmp::Ordering;
3-
use core::intrinsics;
4-
use core::mem;
53
use core::ops::Bound::{Excluded, Included, Unbounded};
64
use core::ops::RangeBounds;
75
use core::ptr;
@@ -304,45 +302,14 @@ macro_rules! def_next_kv_uncheched_dealloc {
304302
def_next_kv_uncheched_dealloc! {unsafe fn next_kv_unchecked_dealloc: right_kv}
305303
def_next_kv_uncheched_dealloc! {unsafe fn next_back_kv_unchecked_dealloc: left_kv}
306304

307-
/// This replaces the value behind the `v` unique reference by calling the
308-
/// relevant function.
309-
///
310-
/// If a panic occurs in the `change` closure, the entire process will be aborted.
311-
#[inline]
312-
fn take_mut<T>(v: &mut T, change: impl FnOnce(T) -> T) {
313-
replace(v, |value| (change(value), ()))
314-
}
315-
316-
/// This replaces the value behind the `v` unique reference by calling the
317-
/// relevant function, and returns a result obtained along the way.
318-
///
319-
/// If a panic occurs in the `change` closure, the entire process will be aborted.
320-
#[inline]
321-
fn replace<T, R>(v: &mut T, change: impl FnOnce(T) -> (T, R)) -> R {
322-
struct PanicGuard;
323-
impl Drop for PanicGuard {
324-
fn drop(&mut self) {
325-
intrinsics::abort()
326-
}
327-
}
328-
let guard = PanicGuard;
329-
let value = unsafe { ptr::read(v) };
330-
let (new_value, ret) = change(value);
331-
unsafe {
332-
ptr::write(v, new_value);
333-
}
334-
mem::forget(guard);
335-
ret
336-
}
337-
338305
impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Edge> {
339306
/// Moves the leaf edge handle to the next leaf edge and returns references to the
340307
/// key and value in between.
341308
///
342309
/// # Safety
343310
/// There must be another KV in the direction travelled.
344311
pub unsafe fn next_unchecked(&mut self) -> (&'a K, &'a V) {
345-
replace(self, |leaf_edge| {
312+
super::mem::replace(self, |leaf_edge| {
346313
let kv = leaf_edge.next_kv();
347314
let kv = unsafe { unwrap_unchecked(kv.ok()) };
348315
(kv.next_leaf_edge(), kv.into_kv())
@@ -355,7 +322,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Ed
355322
/// # Safety
356323
/// There must be another KV in the direction travelled.
357324
pub unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a V) {
358-
replace(self, |leaf_edge| {
325+
super::mem::replace(self, |leaf_edge| {
359326
let kv = leaf_edge.next_back_kv();
360327
let kv = unsafe { unwrap_unchecked(kv.ok()) };
361328
(kv.next_back_leaf_edge(), kv.into_kv())
@@ -370,7 +337,7 @@ impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::E
370337
/// # Safety
371338
/// There must be another KV in the direction travelled.
372339
pub unsafe fn next_unchecked(&mut self) -> (&'a K, &'a mut V) {
373-
let kv = replace(self, |leaf_edge| {
340+
let kv = super::mem::replace(self, |leaf_edge| {
374341
let kv = leaf_edge.next_kv();
375342
let kv = unsafe { unwrap_unchecked(kv.ok()) };
376343
(unsafe { ptr::read(&kv) }.next_leaf_edge(), kv)
@@ -385,7 +352,7 @@ impl<'a, K, V> Handle<NodeRef<marker::ValMut<'a>, K, V, marker::Leaf>, marker::E
385352
/// # Safety
386353
/// There must be another KV in the direction travelled.
387354
pub unsafe fn next_back_unchecked(&mut self) -> (&'a K, &'a mut V) {
388-
let kv = replace(self, |leaf_edge| {
355+
let kv = super::mem::replace(self, |leaf_edge| {
389356
let kv = leaf_edge.next_back_kv();
390357
let kv = unsafe { unwrap_unchecked(kv.ok()) };
391358
(unsafe { ptr::read(&kv) }.next_back_leaf_edge(), kv)
@@ -401,7 +368,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
401368
/// # Safety
402369
/// There must be another KV in the direction travelled.
403370
pub unsafe fn move_next_unchecked(&mut self) {
404-
take_mut(self, |leaf_edge| {
371+
super::mem::take_mut(self, |leaf_edge| {
405372
let kv = leaf_edge.next_kv();
406373
let kv = unsafe { unwrap_unchecked(kv.ok()) };
407374
kv.next_leaf_edge()
@@ -423,7 +390,7 @@ impl<K, V> Handle<NodeRef<marker::Owned, K, V, marker::Leaf>, marker::Edge> {
423390
/// call this method again subject to its safety conditions, or call counterpart
424391
/// `next_back_unchecked` subject to its safety conditions.
425392
pub unsafe fn next_unchecked(&mut self) -> (K, V) {
426-
replace(self, |leaf_edge| {
393+
super::mem::replace(self, |leaf_edge| {
427394
let kv = unsafe { next_kv_unchecked_dealloc(leaf_edge) };
428395
let k = unsafe { ptr::read(kv.reborrow().into_kv().0) };
429396
let v = unsafe { ptr::read(kv.reborrow().into_kv().1) };
@@ -444,7 +411,7 @@ impl<K, V> Handle<NodeRef<marker::Owned, K, V, marker::Leaf>, marker::Edge> {
444411
/// call this method again subject to its safety conditions, or call counterpart
445412
/// `next_unchecked` subject to its safety conditions.
446413
pub unsafe fn next_back_unchecked(&mut self) -> (K, V) {
447-
replace(self, |leaf_edge| {
414+
super::mem::replace(self, |leaf_edge| {
448415
let kv = unsafe { next_back_kv_unchecked_dealloc(leaf_edge) };
449416
let k = unsafe { ptr::read(kv.reborrow().into_kv().0) };
450417
let v = unsafe { ptr::read(kv.reborrow().into_kv().1) };

0 commit comments

Comments
 (0)