@@ -442,6 +442,24 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
442
442
// SAFETY: we have exclusive access to the entire node.
443
443
unsafe { & mut * ptr }
444
444
}
445
+
446
+ /// Returns a dormant copy of this node with its lifetime erased which can
447
+ /// be reawakened later.
448
+ pub fn dormant ( & self ) -> NodeRef < marker:: DormantMut , K , V , Type > {
449
+ NodeRef { height : self . height , node : self . node , _marker : PhantomData }
450
+ }
451
+ }
452
+
453
+ impl < K , V , Type > NodeRef < marker:: DormantMut , K , V , Type > {
454
+ /// Revert to the unique borrow initially captured.
455
+ ///
456
+ /// # Safety
457
+ ///
458
+ /// The reborrow must have ended, i.e., the reference returned by `new` and
459
+ /// all pointers and references derived from it, must not be used anymore.
460
+ pub unsafe fn awaken < ' a > ( self ) -> NodeRef < marker:: Mut < ' a > , K , V , Type > {
461
+ NodeRef { height : self . height , node : self . node , _marker : PhantomData }
462
+ }
445
463
}
446
464
447
465
impl < K , V , Type > NodeRef < marker:: Dying , K , V , Type > {
@@ -798,6 +816,25 @@ impl<'a, K, V, NodeType, HandleType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeT
798
816
// We can't use Handle::new_kv or Handle::new_edge because we don't know our type
799
817
Handle { node : unsafe { self . node . reborrow_mut ( ) } , idx : self . idx , _marker : PhantomData }
800
818
}
819
+
820
+ /// Returns a dormant copy of this handle which can be reawakened later.
821
+ ///
822
+ /// See [`DormantMutRef`] for more details.
823
+ pub fn dormant ( & self ) -> Handle < NodeRef < marker:: DormantMut , K , V , NodeType > , HandleType > {
824
+ Handle { node : self . node . dormant ( ) , idx : self . idx , _marker : PhantomData }
825
+ }
826
+ }
827
+
828
+ impl < K , V , NodeType , HandleType > Handle < NodeRef < marker:: DormantMut , K , V , NodeType > , HandleType > {
829
+ /// Revert to the unique borrow initially captured.
830
+ ///
831
+ /// # Safety
832
+ ///
833
+ /// The reborrow must have ended, i.e., the reference returned by `new` and
834
+ /// all pointers and references derived from it, must not be used anymore.
835
+ pub unsafe fn awaken < ' a > ( self ) -> Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , HandleType > {
836
+ Handle { node : unsafe { self . node . awaken ( ) } , idx : self . idx , _marker : PhantomData }
837
+ }
801
838
}
802
839
803
840
impl < BorrowType , K , V , NodeType > Handle < NodeRef < BorrowType , K , V , NodeType > , marker:: Edge > {
@@ -851,9 +888,11 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
851
888
/// Inserts a new key-value pair between the key-value pairs to the right and left of
852
889
/// this edge. This method assumes that there is enough space in the node for the new
853
890
/// pair to fit.
854
- ///
855
- /// The returned pointer points to the inserted value.
856
- fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
891
+ unsafe fn insert_fit (
892
+ mut self ,
893
+ key : K ,
894
+ val : V ,
895
+ ) -> Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: KV > {
857
896
debug_assert ! ( self . node. len( ) < CAPACITY ) ;
858
897
let new_len = self . node . len ( ) + 1 ;
859
898
@@ -862,7 +901,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
862
901
slice_insert ( self . node . val_area_mut ( ..new_len) , self . idx , val) ;
863
902
* self . node . len_mut ( ) = new_len as u16 ;
864
903
865
- self . node . val_area_mut ( self . idx ) . assume_init_mut ( )
904
+ Handle :: new_kv ( self . node , self . idx )
866
905
}
867
906
}
868
907
}
@@ -871,30 +910,37 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
871
910
/// Inserts a new key-value pair between the key-value pairs to the right and left of
872
911
/// this edge. This method splits the node if there isn't enough room.
873
912
///
874
- /// The returned pointer points to the inserted value.
913
+ /// Returns a dormant handle to the inserted node which can be reawakened
914
+ /// once splitting is complete.
875
915
fn insert < A : Allocator + Clone > (
876
- mut self ,
916
+ self ,
877
917
key : K ,
878
918
val : V ,
879
919
alloc : A ,
880
- ) -> ( Option < SplitResult < ' a , K , V , marker:: Leaf > > , * mut V ) {
920
+ ) -> (
921
+ Option < SplitResult < ' a , K , V , marker:: Leaf > > ,
922
+ Handle < NodeRef < marker:: DormantMut , K , V , marker:: Leaf > , marker:: KV > ,
923
+ ) {
881
924
if self . node . len ( ) < CAPACITY {
882
- let val_ptr = self . insert_fit ( key, val) ;
883
- ( None , val_ptr)
925
+ // SAFETY: There is enough space in the node for insertion.
926
+ let handle = unsafe { self . insert_fit ( key, val) } ;
927
+ ( None , handle. dormant ( ) )
884
928
} else {
885
929
let ( middle_kv_idx, insertion) = splitpoint ( self . idx ) ;
886
930
let middle = unsafe { Handle :: new_kv ( self . node , middle_kv_idx) } ;
887
931
let mut result = middle. split ( alloc) ;
888
- let mut insertion_edge = match insertion {
932
+ let insertion_edge = match insertion {
889
933
LeftOrRight :: Left ( insert_idx) => unsafe {
890
934
Handle :: new_edge ( result. left . reborrow_mut ( ) , insert_idx)
891
935
} ,
892
936
LeftOrRight :: Right ( insert_idx) => unsafe {
893
937
Handle :: new_edge ( result. right . borrow_mut ( ) , insert_idx)
894
938
} ,
895
939
} ;
896
- let val_ptr = insertion_edge. insert_fit ( key, val) ;
897
- ( Some ( result) , val_ptr)
940
+ // SAFETY: We just split the node, so there is enough space for
941
+ // insertion.
942
+ let handle = unsafe { insertion_edge. insert_fit ( key, val) . dormant ( ) } ;
943
+ ( Some ( result) , handle)
898
944
}
899
945
}
900
946
}
@@ -976,21 +1022,31 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
976
1022
key : K ,
977
1023
value : V ,
978
1024
alloc : A ,
979
- ) -> ( Option < SplitResult < ' a , K , V , marker:: LeafOrInternal > > , * mut V ) {
980
- let ( mut split, val_ptr) = match self . insert ( key, value, alloc. clone ( ) ) {
981
- ( None , val_ptr) => return ( None , val_ptr) ,
982
- ( Some ( split) , val_ptr) => ( split. forget_node_type ( ) , val_ptr) ,
1025
+ split_root : impl FnOnce ( SplitResult < ' a , K , V , marker:: LeafOrInternal > ) ,
1026
+ ) -> Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: KV > {
1027
+ let ( mut split, handle) = match self . insert ( key, value, alloc. clone ( ) ) {
1028
+ // SAFETY: we have finished splitting and can now re-awaken the
1029
+ // handle to the inserted element.
1030
+ ( None , handle) => return unsafe { handle. awaken ( ) } ,
1031
+ ( Some ( split) , handle) => ( split. forget_node_type ( ) , handle) ,
983
1032
} ;
984
1033
985
1034
loop {
986
1035
split = match split. left . ascend ( ) {
987
1036
Ok ( parent) => {
988
1037
match parent. insert ( split. kv . 0 , split. kv . 1 , split. right , alloc. clone ( ) ) {
989
- None => return ( None , val_ptr) ,
1038
+ // SAFETY: we have finished splitting and can now re-awaken the
1039
+ // handle to the inserted element.
1040
+ None => return unsafe { handle. awaken ( ) } ,
990
1041
Some ( split) => split. forget_node_type ( ) ,
991
1042
}
992
1043
}
993
- Err ( root) => return ( Some ( SplitResult { left : root, ..split } ) , val_ptr) ,
1044
+ Err ( root) => {
1045
+ split_root ( SplitResult { left : root, ..split } ) ;
1046
+ // SAFETY: we have finished splitting and can now re-awaken the
1047
+ // handle to the inserted element.
1048
+ return unsafe { handle. awaken ( ) } ;
1049
+ }
994
1050
} ;
995
1051
}
996
1052
}
@@ -1667,6 +1723,7 @@ pub mod marker {
1667
1723
1668
1724
pub enum Owned { }
1669
1725
pub enum Dying { }
1726
+ pub enum DormantMut { }
1670
1727
pub struct Immut < ' a > ( PhantomData < & ' a ( ) > ) ;
1671
1728
pub struct Mut < ' a > ( PhantomData < & ' a mut ( ) > ) ;
1672
1729
pub struct ValMut < ' a > ( PhantomData < & ' a mut ( ) > ) ;
@@ -1688,6 +1745,7 @@ pub mod marker {
1688
1745
impl < ' a > BorrowType for Immut < ' a > { }
1689
1746
impl < ' a > BorrowType for Mut < ' a > { }
1690
1747
impl < ' a > BorrowType for ValMut < ' a > { }
1748
+ impl BorrowType for DormantMut { }
1691
1749
1692
1750
pub enum KV { }
1693
1751
pub enum Edge { }
0 commit comments