@@ -35,46 +35,42 @@ impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
3535}
3636
3737impl < ' 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.
3840 #[ inline]
3941 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 ( ) ) }
4143 }
4244
45+ /// Moves the leaf edge handle to the previous leaf edge and returns
46+ /// references to the key and value in between.
4347 #[ inline]
4448 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 ( ) ) }
5650 }
5751}
5852
5953impl < ' 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.
6056 #[ inline]
6157 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+ }
6363 }
6464
65+ /// Moves the leaf edge handle to the previous leaf edge and returns
66+ /// references to the key and value in between.
6567 #[ inline]
6668 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+ }
7874 }
7975}
8076
@@ -89,19 +85,15 @@ impl<K, V> LeafRange<marker::Dying, K, V> {
8985 #[ inline]
9086 pub unsafe fn deallocating_next_unchecked (
9187 & 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 ) }
9690 }
9791
9892 #[ inline]
9993 pub unsafe fn deallocating_next_back_unchecked (
10094 & 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 ) }
10597 }
10698}
10799
@@ -388,100 +380,80 @@ impl<K, V> Handle<NodeRef<marker::Dying, K, V, marker::Leaf>, marker::Edge> {
388380 }
389381}
390382
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+ } )
415399}
416400
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+ } )
445417}
446418
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+ }
465438
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+ } )
485457}
486458
487459impl < BorrowType : marker:: BorrowType , K , V > NodeRef < BorrowType , K , V , marker:: LeafOrInternal > {
0 commit comments