@@ -212,7 +212,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
212
212
let mut out_tree = clone_subtree ( internal. first_edge ( ) . descend ( ) ) ;
213
213
214
214
{
215
- let out_root = BTreeMap :: ensure_is_owned ( & mut out_tree. root ) ;
215
+ let out_root = out_tree. root . as_mut ( ) . unwrap ( ) ;
216
216
let mut out_node = out_root. push_internal_level ( ) ;
217
217
let mut in_edge = internal. first_edge ( ) ;
218
218
while let Ok ( kv) = in_edge. right_kv ( ) {
@@ -278,11 +278,12 @@ where
278
278
279
279
fn replace ( & mut self , key : K ) -> Option < K > {
280
280
let ( map, dormant_map) = DormantMutRef :: new ( self ) ;
281
- let root_node = Self :: ensure_is_owned ( & mut map. root ) . borrow_mut ( ) ;
281
+ let root_node = map. root . get_or_insert_with ( Root :: new ) . borrow_mut ( ) ;
282
282
match root_node. search_tree :: < K > ( & key) {
283
283
Found ( mut kv) => Some ( mem:: replace ( kv. key_mut ( ) , key) ) ,
284
284
GoDown ( handle) => {
285
- VacantEntry { key, handle, dormant_map, _marker : PhantomData } . insert ( ( ) ) ;
285
+ VacantEntry { key, handle : Some ( handle) , dormant_map, _marker : PhantomData }
286
+ . insert ( ( ) ) ;
286
287
None
287
288
}
288
289
}
@@ -1032,7 +1033,7 @@ impl<K, V> BTreeMap<K, V> {
1032
1033
1033
1034
let self_iter = mem:: take ( self ) . into_iter ( ) ;
1034
1035
let other_iter = mem:: take ( other) . into_iter ( ) ;
1035
- let root = BTreeMap :: ensure_is_owned ( & mut self . root ) ;
1036
+ let root = self . root . get_or_insert_with ( Root :: new ) ;
1036
1037
root. append_from_sorted_iters ( self_iter, other_iter, & mut self . length )
1037
1038
}
1038
1039
@@ -1144,14 +1145,20 @@ impl<K, V> BTreeMap<K, V> {
1144
1145
where
1145
1146
K : Ord ,
1146
1147
{
1147
- // FIXME(@porglezomp) Avoid allocating if we don't insert
1148
1148
let ( map, dormant_map) = DormantMutRef :: new ( self ) ;
1149
- let root_node = Self :: ensure_is_owned ( & mut map. root ) . borrow_mut ( ) ;
1150
- match root_node. search_tree ( & key) {
1151
- Found ( handle) => Occupied ( OccupiedEntry { handle, dormant_map, _marker : PhantomData } ) ,
1152
- GoDown ( handle) => {
1153
- Vacant ( VacantEntry { key, handle, dormant_map, _marker : PhantomData } )
1154
- }
1149
+ match map. root {
1150
+ None => Vacant ( VacantEntry { key, handle : None , dormant_map, _marker : PhantomData } ) ,
1151
+ Some ( ref mut root) => match root. borrow_mut ( ) . search_tree ( & key) {
1152
+ Found ( handle) => {
1153
+ Occupied ( OccupiedEntry { handle, dormant_map, _marker : PhantomData } )
1154
+ }
1155
+ GoDown ( handle) => Vacant ( VacantEntry {
1156
+ key,
1157
+ handle : Some ( handle) ,
1158
+ dormant_map,
1159
+ _marker : PhantomData ,
1160
+ } ) ,
1161
+ } ,
1155
1162
}
1156
1163
}
1157
1164
@@ -2247,12 +2254,6 @@ impl<K, V> BTreeMap<K, V> {
2247
2254
pub const fn is_empty ( & self ) -> bool {
2248
2255
self . len ( ) == 0
2249
2256
}
2250
-
2251
- /// If the root node is the empty (non-allocated) root node, allocate our
2252
- /// own node. Is an associated function to avoid borrowing the entire BTreeMap.
2253
- fn ensure_is_owned ( root : & mut Option < Root < K , V > > ) -> & mut Root < K , V > {
2254
- root. get_or_insert_with ( Root :: new)
2255
- }
2256
2257
}
2257
2258
2258
2259
#[ cfg( test) ]
0 commit comments