@@ -212,7 +212,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
212212 let mut out_tree = clone_subtree ( internal. first_edge ( ) . descend ( ) ) ;
213213
214214 {
215- let out_root = BTreeMap :: ensure_is_owned ( & mut out_tree. root ) ;
215+ let out_root = out_tree. root . as_mut ( ) . unwrap ( ) ;
216216 let mut out_node = out_root. push_internal_level ( ) ;
217217 let mut in_edge = internal. first_edge ( ) ;
218218 while let Ok ( kv) = in_edge. right_kv ( ) {
@@ -278,11 +278,12 @@ where
278278
279279 fn replace ( & mut self , key : K ) -> Option < K > {
280280 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 ( ) ;
282282 match root_node. search_tree :: < K > ( & key) {
283283 Found ( mut kv) => Some ( mem:: replace ( kv. key_mut ( ) , key) ) ,
284284 GoDown ( handle) => {
285- VacantEntry { key, handle, dormant_map, _marker : PhantomData } . insert ( ( ) ) ;
285+ VacantEntry { key, handle : Some ( handle) , dormant_map, _marker : PhantomData }
286+ . insert ( ( ) ) ;
286287 None
287288 }
288289 }
@@ -1032,7 +1033,7 @@ impl<K, V> BTreeMap<K, V> {
10321033
10331034 let self_iter = mem:: take ( self ) . into_iter ( ) ;
10341035 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 ) ;
10361037 root. append_from_sorted_iters ( self_iter, other_iter, & mut self . length )
10371038 }
10381039
@@ -1144,14 +1145,20 @@ impl<K, V> BTreeMap<K, V> {
11441145 where
11451146 K : Ord ,
11461147 {
1147- // FIXME(@porglezomp) Avoid allocating if we don't insert
11481148 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+ } ,
11551162 }
11561163 }
11571164
@@ -2247,12 +2254,6 @@ impl<K, V> BTreeMap<K, V> {
22472254 pub const fn is_empty ( & self ) -> bool {
22482255 self . len ( ) == 0
22492256 }
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- }
22562257}
22572258
22582259#[ cfg( test) ]
0 commit comments