@@ -64,8 +64,10 @@ macro_rules! def_next_kv_uncheched_dealloc {
6464 edge = match edge. $adjacent_kv( ) {
6565 Ok ( internal_kv) => return internal_kv,
6666 Err ( last_edge) => {
67- let parent_edge = last_edge. into_node( ) . deallocate_and_ascend( ) ;
68- unwrap_unchecked( parent_edge) . forget_node_type( )
67+ unsafe {
68+ let parent_edge = last_edge. into_node( ) . deallocate_and_ascend( ) ;
69+ unwrap_unchecked( parent_edge) . forget_node_type( )
70+ }
6971 }
7072 }
7173 }
@@ -82,9 +84,11 @@ def_next_kv_uncheched_dealloc! {unsafe fn next_back_kv_unchecked_dealloc: left_k
8284/// Safety: The change closure must not panic.
8385#[ inline]
8486unsafe fn replace < T , R > ( v : & mut T , change : impl FnOnce ( T ) -> ( T , R ) ) -> R {
85- let value = ptr:: read ( v) ;
87+ let value = unsafe { ptr:: read ( v) } ;
8688 let ( new_value, ret) = change ( value) ;
87- ptr:: write ( v, new_value) ;
89+ unsafe {
90+ ptr:: write ( v, new_value) ;
91+ }
8892 ret
8993}
9094
@@ -93,22 +97,26 @@ impl<'a, K, V> Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Ed
9397 /// key and value in between.
9498 /// Unsafe because the caller must ensure that the leaf edge is not the last one in the tree.
9599 pub unsafe fn next_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
96- replace ( self , |leaf_edge| {
97- let kv = leaf_edge. next_kv ( ) ;
98- let kv = unwrap_unchecked ( kv. ok ( ) ) ;
99- ( kv. next_leaf_edge ( ) , kv. into_kv ( ) )
100- } )
100+ unsafe {
101+ replace ( self , |leaf_edge| {
102+ let kv = leaf_edge. next_kv ( ) ;
103+ let kv = unwrap_unchecked ( kv. ok ( ) ) ;
104+ ( kv. next_leaf_edge ( ) , kv. into_kv ( ) )
105+ } )
106+ }
101107 }
102108
103109 /// Moves the leaf edge handle to the previous leaf edge and returns references to the
104110 /// key and value in between.
105111 /// Unsafe because the caller must ensure that the leaf edge is not the first one in the tree.
106112 pub unsafe fn next_back_unchecked ( & mut self ) -> ( & ' a K , & ' a V ) {
107- replace ( self , |leaf_edge| {
108- let kv = leaf_edge. next_back_kv ( ) ;
109- let kv = unwrap_unchecked ( kv. ok ( ) ) ;
110- ( kv. next_back_leaf_edge ( ) , kv. into_kv ( ) )
111- } )
113+ unsafe {
114+ replace ( self , |leaf_edge| {
115+ let kv = leaf_edge. next_back_kv ( ) ;
116+ let kv = unwrap_unchecked ( kv. ok ( ) ) ;
117+ ( kv. next_back_leaf_edge ( ) , kv. into_kv ( ) )
118+ } )
119+ }
112120 }
113121}
114122
@@ -119,14 +127,16 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
119127 /// - The caller must ensure that the leaf edge is not the last one in the tree.
120128 /// - Using the updated handle may well invalidate the returned references.
121129 pub unsafe fn next_unchecked ( & mut self ) -> ( & ' a mut K , & ' a mut V ) {
122- let kv = replace ( self , |leaf_edge| {
123- let kv = leaf_edge. next_kv ( ) ;
124- let kv = unwrap_unchecked ( kv. ok ( ) ) ;
125- ( ptr:: read ( & kv) . next_leaf_edge ( ) , kv)
126- } ) ;
127- // Doing the descend (and perhaps another move) invalidates the references
128- // returned by `into_kv_mut`, so we have to do this last.
129- kv. into_kv_mut ( )
130+ unsafe {
131+ let kv = replace ( self , |leaf_edge| {
132+ let kv = leaf_edge. next_kv ( ) ;
133+ let kv = unwrap_unchecked ( kv. ok ( ) ) ;
134+ ( ptr:: read ( & kv) . next_leaf_edge ( ) , kv)
135+ } ) ;
136+ // Doing the descend (and perhaps another move) invalidates the references
137+ // returned by `into_kv_mut`, so we have to do this last.
138+ kv. into_kv_mut ( )
139+ }
130140 }
131141
132142 /// Moves the leaf edge handle to the previous leaf and returns references to the
@@ -135,14 +145,16 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
135145 /// - The caller must ensure that the leaf edge is not the first one in the tree.
136146 /// - Using the updated handle may well invalidate the returned references.
137147 pub unsafe fn next_back_unchecked ( & mut self ) -> ( & ' a mut K , & ' a mut V ) {
138- let kv = replace ( self , |leaf_edge| {
139- let kv = leaf_edge. next_back_kv ( ) ;
140- let kv = unwrap_unchecked ( kv. ok ( ) ) ;
141- ( ptr:: read ( & kv) . next_back_leaf_edge ( ) , kv)
142- } ) ;
143- // Doing the descend (and perhaps another move) invalidates the references
144- // returned by `into_kv_mut`, so we have to do this last.
145- kv. into_kv_mut ( )
148+ unsafe {
149+ let kv = replace ( self , |leaf_edge| {
150+ let kv = leaf_edge. next_back_kv ( ) ;
151+ let kv = unwrap_unchecked ( kv. ok ( ) ) ;
152+ ( ptr:: read ( & kv) . next_back_leaf_edge ( ) , kv)
153+ } ) ;
154+ // Doing the descend (and perhaps another move) invalidates the references
155+ // returned by `into_kv_mut`, so we have to do this last.
156+ kv. into_kv_mut ( )
157+ }
146158 }
147159}
148160
@@ -159,12 +171,14 @@ impl<K, V> Handle<NodeRef<marker::Owned, K, V, marker::Leaf>, marker::Edge> {
159171 /// if the two preconditions above hold.
160172 /// - Using the updated handle may well invalidate the returned references.
161173 pub unsafe fn next_unchecked ( & mut self ) -> ( K , V ) {
162- replace ( self , |leaf_edge| {
163- let kv = next_kv_unchecked_dealloc ( leaf_edge) ;
164- let k = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 0 ) ;
165- let v = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 1 ) ;
166- ( kv. next_leaf_edge ( ) , ( k, v) )
167- } )
174+ unsafe {
175+ replace ( self , |leaf_edge| {
176+ let kv = next_kv_unchecked_dealloc ( leaf_edge) ;
177+ let k = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 0 ) ;
178+ let v = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 1 ) ;
179+ ( kv. next_leaf_edge ( ) , ( k, v) )
180+ } )
181+ }
168182 }
169183
170184 /// Moves the leaf edge handle to the previous leaf edge and returns the key
@@ -179,12 +193,14 @@ impl<K, V> Handle<NodeRef<marker::Owned, K, V, marker::Leaf>, marker::Edge> {
179193 /// if the two preconditions above hold.
180194 /// - Using the updated handle may well invalidate the returned references.
181195 pub unsafe fn next_back_unchecked ( & mut self ) -> ( K , V ) {
182- replace ( self , |leaf_edge| {
183- let kv = next_back_kv_unchecked_dealloc ( leaf_edge) ;
184- let k = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 0 ) ;
185- let v = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 1 ) ;
186- ( kv. next_back_leaf_edge ( ) , ( k, v) )
187- } )
196+ unsafe {
197+ replace ( self , |leaf_edge| {
198+ let kv = next_back_kv_unchecked_dealloc ( leaf_edge) ;
199+ let k = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 0 ) ;
200+ let v = ptr:: read ( kv. reborrow ( ) . into_kv ( ) . 1 ) ;
201+ ( kv. next_back_leaf_edge ( ) , ( k, v) )
202+ } )
203+ }
188204 }
189205}
190206
0 commit comments