@@ -35,46 +35,42 @@ impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
35
35
}
36
36
37
37
impl < ' a , K , V > LeafRange < marker:: Immut < ' a > , K , V > {
38
+ /// Moves the leaf edge handle to the next leaf edge and returns
39
+ /// references to the key and value in between.
38
40
#[ inline]
39
41
pub fn next_checked ( & mut self ) -> Option < ( & ' a K , & ' a V ) > {
40
- if self . is_empty ( ) { None } else { Some ( unsafe { self . next_unchecked ( ) } ) }
42
+ if self . is_empty ( ) { None } else { perform_next ( & mut self . front , |kv| kv . into_kv ( ) ) }
41
43
}
42
44
45
+ /// Moves the leaf edge handle to the previous leaf edge and returns
46
+ /// references to the key and value in between.
43
47
#[ inline]
44
48
pub fn next_back_checked ( & mut self ) -> Option < ( & ' a K , & ' a V ) > {
45
- if self . is_empty ( ) { None } else { Some ( unsafe { self . next_back_unchecked ( ) } ) }
46
- }
47
-
48
- #[ inline]
49
- pub unsafe fn next_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
50
- unsafe { self . front . as_mut ( ) . unwrap ( ) . next_unchecked ( ) }
51
- }
52
-
53
- #[ inline]
54
- pub unsafe fn next_back_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
55
- unsafe { self . back . as_mut ( ) . unwrap ( ) . next_back_unchecked ( ) }
49
+ if self . is_empty ( ) { None } else { perform_next_back ( & mut self . back , |kv| kv. into_kv ( ) ) }
56
50
}
57
51
}
58
52
59
53
impl < ' a , K , V > LeafRange < marker:: ValMut < ' a > , K , V > {
54
+ /// Moves the leaf edge handle to the next leaf edge and returns
55
+ /// references to the key and value in between.
60
56
#[ inline]
61
57
pub fn next_checked ( & mut self ) -> Option < ( & ' a K , & ' a mut V ) > {
62
- if self . is_empty ( ) { None } else { Some ( unsafe { self . next_unchecked ( ) } ) }
58
+ if self . is_empty ( ) {
59
+ None
60
+ } else {
61
+ perform_next ( & mut self . front , |kv| unsafe { ptr:: read ( kv) . into_kv_valmut ( ) } )
62
+ }
63
63
}
64
64
65
+ /// Moves the leaf edge handle to the previous leaf edge and returns
66
+ /// references to the key and value in between.
65
67
#[ inline]
66
68
pub fn next_back_checked ( & mut self ) -> Option < ( & ' a K , & ' a mut V ) > {
67
- if self . is_empty ( ) { None } else { Some ( unsafe { self . next_back_unchecked ( ) } ) }
68
- }
69
-
70
- #[ inline]
71
- pub unsafe fn next_unchecked ( & mut self ) -> ( & ' a K , & ' a mut V ) {
72
- unsafe { self . front . as_mut ( ) . unwrap ( ) . next_unchecked ( ) }
73
- }
74
-
75
- #[ inline]
76
- pub unsafe fn next_back_unchecked ( & mut self ) -> ( & ' a K , & ' a mut V ) {
77
- unsafe { self . back . as_mut ( ) . unwrap ( ) . next_back_unchecked ( ) }
69
+ if self . is_empty ( ) {
70
+ None
71
+ } else {
72
+ perform_next_back ( & mut self . back , |kv| unsafe { ptr:: read ( kv) . into_kv_valmut ( ) } )
73
+ }
78
74
}
79
75
}
80
76
@@ -89,19 +85,15 @@ impl<K, V> LeafRange<marker::Dying, K, V> {
89
85
#[ inline]
90
86
pub unsafe fn deallocating_next_unchecked (
91
87
& mut self ,
92
- ) -> Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > {
93
- debug_assert ! ( self . front. is_some( ) ) ;
94
- let front = self . front . as_mut ( ) . unwrap ( ) ;
95
- unsafe { front. deallocating_next_unchecked ( ) }
88
+ ) -> Option < Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > > {
89
+ unsafe { deallocating_next_unchecked ( & mut self . front ) }
96
90
}
97
91
98
92
#[ inline]
99
93
pub unsafe fn deallocating_next_back_unchecked (
100
94
& mut self ,
101
- ) -> Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > {
102
- debug_assert ! ( self . back. is_some( ) ) ;
103
- let back = self . back . as_mut ( ) . unwrap ( ) ;
104
- unsafe { back. deallocating_next_back_unchecked ( ) }
95
+ ) -> Option < Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > > {
96
+ unsafe { deallocating_next_back_unchecked ( & mut self . back ) }
105
97
}
106
98
}
107
99
@@ -388,100 +380,80 @@ impl<K, V> Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge> {
388
380
}
389
381
}
390
382
391
- impl < ' a , K , V > Handle < NodeRef < marker:: Immut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
392
- /// Moves the leaf edge handle to the next leaf edge and returns references to the
393
- /// key and value in between.
394
- ///
395
- /// # Safety
396
- /// There must be another KV in the direction travelled.
397
- unsafe fn next_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
398
- super :: mem:: replace ( self , |leaf_edge| {
399
- let kv = leaf_edge. next_kv ( ) . ok ( ) . unwrap ( ) ;
400
- ( kv. next_leaf_edge ( ) , kv. into_kv ( ) )
401
- } )
402
- }
403
-
404
- /// Moves the leaf edge handle to the previous leaf edge and returns references to the
405
- /// key and value in between.
406
- ///
407
- /// # Safety
408
- /// There must be another KV in the direction travelled.
409
- unsafe fn next_back_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
410
- super :: mem:: replace ( self , |leaf_edge| {
411
- let kv = leaf_edge. next_back_kv ( ) . ok ( ) . unwrap ( ) ;
412
- ( kv. next_back_leaf_edge ( ) , kv. into_kv ( ) )
413
- } )
414
- }
383
+ /// If possible, extract some result from the next KV and move to the edge beyond it.
384
+ fn perform_next < BorrowType : marker:: BorrowType , K , V , F , R > (
385
+ front : & mut Option < Handle < NodeRef < BorrowType , K , V , marker:: Leaf > , marker:: Edge > > ,
386
+ f : F ,
387
+ ) -> Option < R >
388
+ where
389
+ F : Fn ( & Handle < NodeRef < BorrowType , K , V , marker:: LeafOrInternal > , marker:: KV > ) -> R ,
390
+ {
391
+ super :: mem:: replace ( front, |edge| {
392
+ if let Some ( kv) = edge. and_then ( |edge| edge. next_kv ( ) . ok ( ) ) {
393
+ let result = f ( & kv) ;
394
+ ( Some ( kv. next_leaf_edge ( ) ) , Some ( result) )
395
+ } else {
396
+ ( None , None )
397
+ }
398
+ } )
415
399
}
416
400
417
- impl < ' a , K , V > Handle < NodeRef < marker:: ValMut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
418
- /// Moves the leaf edge handle to the next leaf edge and returns references to the
419
- /// key and value in between.
420
- ///
421
- /// # Safety
422
- /// There must be another KV in the direction travelled.
423
- unsafe fn next_unchecked ( & mut self ) -> ( & ' a K , & ' a mut V ) {
424
- let kv = super :: mem:: replace ( self , |leaf_edge| {
425
- let kv = leaf_edge. next_kv ( ) . ok ( ) . unwrap ( ) ;
426
- ( unsafe { ptr:: read ( & kv) } . next_leaf_edge ( ) , kv)
427
- } ) ;
428
- // Doing this last is faster, according to benchmarks.
429
- kv. into_kv_valmut ( )
430
- }
431
-
432
- /// Moves the leaf edge handle to the previous leaf and returns references to the
433
- /// key and value in between.
434
- ///
435
- /// # Safety
436
- /// There must be another KV in the direction travelled.
437
- unsafe fn next_back_unchecked ( & mut self ) -> ( & ' a K , & ' a mut V ) {
438
- let kv = super :: mem:: replace ( self , |leaf_edge| {
439
- let kv = leaf_edge. next_back_kv ( ) . ok ( ) . unwrap ( ) ;
440
- ( unsafe { ptr:: read ( & kv) } . next_back_leaf_edge ( ) , kv)
441
- } ) ;
442
- // Doing this last is faster, according to benchmarks.
443
- kv. into_kv_valmut ( )
444
- }
401
+ /// If possible, extract some result from the previous KV and move to the edge beyond it.
402
+ fn perform_next_back < BorrowType : marker:: BorrowType , K , V , F , R > (
403
+ back : & mut Option < Handle < NodeRef < BorrowType , K , V , marker:: Leaf > , marker:: Edge > > ,
404
+ f : F ,
405
+ ) -> Option < R >
406
+ where
407
+ F : Fn ( & Handle < NodeRef < BorrowType , K , V , marker:: LeafOrInternal > , marker:: KV > ) -> R ,
408
+ {
409
+ super :: mem:: replace ( back, |edge| {
410
+ if let Some ( kv) = edge. and_then ( |edge| edge. next_back_kv ( ) . ok ( ) ) {
411
+ let result = f ( & kv) ;
412
+ ( Some ( kv. next_back_leaf_edge ( ) ) , Some ( result) )
413
+ } else {
414
+ ( None , None )
415
+ }
416
+ } )
445
417
}
446
418
447
- impl < K , V > Handle < NodeRef < marker:: Dying , K , V , marker:: Leaf > , marker:: Edge > {
448
- /// Moves the leaf edge handle to the next leaf edge and returns the key and value
449
- /// in between, deallocating any node left behind while leaving the corresponding
450
- /// edge in its parent node dangling.
451
- ///
452
- /// # Safety
453
- /// - There must be another KV in the direction travelled.
454
- /// - That KV was not previously returned by counterpart
455
- /// `deallocating_next_back_unchecked` on any copy of the handles
456
- /// being used to traverse the tree.
457
- ///
458
- /// The only safe way to proceed with the updated handle is to compare it, drop it,
459
- /// or call this method or counterpart `deallocating_next_back_unchecked` again.
460
- pub unsafe fn deallocating_next_unchecked (
461
- & mut self ,
462
- ) -> Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > {
463
- super :: mem:: replace ( self , |leaf_edge| unsafe { leaf_edge. deallocating_next ( ) . unwrap ( ) } )
464
- }
419
+ /// Moves the leaf edge handle to the next leaf edge and returns the key and value
420
+ /// in between, deallocating any node left behind while leaving the corresponding
421
+ /// edge in its parent node dangling.
422
+ ///
423
+ /// # Safety
424
+ /// - The next KV, if any, was not previously returned by counterpart
425
+ /// `deallocating_next_back_unchecked` on any copy of the handles
426
+ /// being used to traverse the tree.
427
+ ///
428
+ /// The only safe way to proceed with the updated handle is to compare it, drop it,
429
+ /// or call this method or counterpart `deallocating_next_back_unchecked` again.
430
+ pub unsafe fn deallocating_next_unchecked < K , V > (
431
+ opt_hndl : & mut Option < Handle < NodeRef < marker:: Dying , K , V , marker:: Leaf > , marker:: Edge > > ,
432
+ ) -> Option < Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > > {
433
+ super :: mem:: replace ( opt_hndl, |edge| {
434
+ edge. and_then ( |edge| unsafe { edge. deallocating_next ( ) } )
435
+ . map_or ( ( None , None ) , |p| ( Some ( p. 0 ) , Some ( p. 1 ) ) )
436
+ } )
437
+ }
465
438
466
- /// Moves the leaf edge handle to the previous leaf edge and returns the key and value
467
- /// in between, deallocating any node left behind while leaving the corresponding
468
- /// edge in its parent node dangling.
469
- ///
470
- /// # Safety
471
- /// - There must be another KV in the direction travelled.
472
- /// - That leaf edge was not previously returned by counterpart
473
- /// `deallocating_next_unchecked` on any copy of the handles
474
- /// being used to traverse the tree.
475
- ///
476
- /// The only safe way to proceed with the updated handle is to compare it, drop it,
477
- /// or call this method or counterpart `deallocating_next_unchecked` again.
478
- unsafe fn deallocating_next_back_unchecked (
479
- & mut self ,
480
- ) -> Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > {
481
- super :: mem:: replace ( self , |leaf_edge| unsafe {
482
- leaf_edge. deallocating_next_back ( ) . unwrap ( )
483
- } )
484
- }
439
+ /// Moves the leaf edge handle to the previous leaf edge and returns the key and value
440
+ /// in between, deallocating any node left behind while leaving the corresponding
441
+ /// edge in its parent node dangling.
442
+ ///
443
+ /// # Safety
444
+ /// - The previous KV, if any, was not previously returned by counterpart
445
+ /// `deallocating_next_unchecked` on any copy of the handles
446
+ /// being used to traverse the tree.
447
+ ///
448
+ /// The only safe way to proceed with the updated handle is to compare it, drop it,
449
+ /// or call this method or counterpart `deallocating_next_unchecked` again.
450
+ unsafe fn deallocating_next_back_unchecked < K , V > (
451
+ opt_hndl : & mut Option < Handle < NodeRef < marker:: Dying , K , V , marker:: Leaf > , marker:: Edge > > ,
452
+ ) -> Option < Handle < NodeRef < marker:: Dying , K , V , marker:: LeafOrInternal > , marker:: KV > > {
453
+ super :: mem:: replace ( opt_hndl, |edge| {
454
+ edge. and_then ( |edge| unsafe { edge. deallocating_next_back ( ) } )
455
+ . map_or ( ( None , None ) , |p| ( Some ( p. 0 ) , Some ( p. 1 ) ) )
456
+ } )
485
457
}
486
458
487
459
impl < BorrowType : marker:: BorrowType , K , V > NodeRef < BorrowType , K , V , marker:: LeafOrInternal > {
0 commit comments