1
1
use core:: borrow:: Borrow ;
2
2
use core:: cmp:: Ordering ;
3
- use core:: intrinsics;
4
- use core:: mem;
5
3
use core:: ops:: Bound :: { Excluded , Included , Unbounded } ;
6
4
use core:: ops:: RangeBounds ;
7
5
use core:: ptr;
@@ -304,45 +302,14 @@ macro_rules! def_next_kv_uncheched_dealloc {
304
302
def_next_kv_uncheched_dealloc ! { unsafe fn next_kv_unchecked_dealloc: right_kv}
305
303
def_next_kv_uncheched_dealloc ! { unsafe fn next_back_kv_unchecked_dealloc: left_kv}
306
304
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
-
338
305
impl < ' a , K , V > Handle < NodeRef < marker:: Immut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
339
306
/// Moves the leaf edge handle to the next leaf edge and returns references to the
340
307
/// key and value in between.
341
308
///
342
309
/// # Safety
343
310
/// There must be another KV in the direction travelled.
344
311
pub unsafe fn next_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
345
- replace ( self , |leaf_edge| {
312
+ super :: mem :: replace ( self , |leaf_edge| {
346
313
let kv = leaf_edge. next_kv ( ) ;
347
314
let kv = unsafe { unwrap_unchecked ( kv. ok ( ) ) } ;
348
315
( 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
355
322
/// # Safety
356
323
/// There must be another KV in the direction travelled.
357
324
pub unsafe fn next_back_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
358
- replace ( self , |leaf_edge| {
325
+ super :: mem :: replace ( self , |leaf_edge| {
359
326
let kv = leaf_edge. next_back_kv ( ) ;
360
327
let kv = unsafe { unwrap_unchecked ( kv. ok ( ) ) } ;
361
328
( 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
370
337
/// # Safety
371
338
/// There must be another KV in the direction travelled.
372
339
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| {
374
341
let kv = leaf_edge. next_kv ( ) ;
375
342
let kv = unsafe { unwrap_unchecked ( kv. ok ( ) ) } ;
376
343
( 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
385
352
/// # Safety
386
353
/// There must be another KV in the direction travelled.
387
354
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| {
389
356
let kv = leaf_edge. next_back_kv ( ) ;
390
357
let kv = unsafe { unwrap_unchecked ( kv. ok ( ) ) } ;
391
358
( 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
401
368
/// # Safety
402
369
/// There must be another KV in the direction travelled.
403
370
pub unsafe fn move_next_unchecked ( & mut self ) {
404
- take_mut ( self , |leaf_edge| {
371
+ super :: mem :: take_mut ( self , |leaf_edge| {
405
372
let kv = leaf_edge. next_kv ( ) ;
406
373
let kv = unsafe { unwrap_unchecked ( kv. ok ( ) ) } ;
407
374
kv. next_leaf_edge ( )
@@ -423,7 +390,7 @@ impl<K, V> Handle<NodeRef<marker::Owned, K, V, marker::Leaf>, marker::Edge> {
423
390
/// call this method again subject to its safety conditions, or call counterpart
424
391
/// `next_back_unchecked` subject to its safety conditions.
425
392
pub unsafe fn next_unchecked ( & mut self ) -> ( K , V ) {
426
- replace ( self , |leaf_edge| {
393
+ super :: mem :: replace ( self , |leaf_edge| {
427
394
let kv = unsafe { next_kv_unchecked_dealloc ( leaf_edge) } ;
428
395
let k = unsafe { ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 0 ) } ;
429
396
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> {
444
411
/// call this method again subject to its safety conditions, or call counterpart
445
412
/// `next_unchecked` subject to its safety conditions.
446
413
pub unsafe fn next_back_unchecked ( & mut self ) -> ( K , V ) {
447
- replace ( self , |leaf_edge| {
414
+ super :: mem :: replace ( self , |leaf_edge| {
448
415
let kv = unsafe { next_back_kv_unchecked_dealloc ( leaf_edge) } ;
449
416
let k = unsafe { ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 0 ) } ;
450
417
let v = unsafe { ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 1 ) } ;
0 commit comments