@@ -151,7 +151,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
151151 let mut out_tree = BTreeMap { root : Some ( node:: Root :: new_leaf ( ) ) , length : 0 } ;
152152
153153 {
154- let root = out_tree. root . as_mut ( ) . unwrap ( ) ;
154+ let root = out_tree. root . as_mut ( ) . unwrap ( ) ; // unwrap succeeds because we just wrapped
155155 let mut out_node = match root. as_mut ( ) . force ( ) {
156156 Leaf ( leaf) => leaf,
157157 Internal ( _) => unreachable ! ( ) ,
@@ -171,14 +171,10 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
171171 }
172172 Internal ( internal) => {
173173 let mut out_tree = clone_subtree ( internal. first_edge ( ) . descend ( ) ) ;
174- out_tree. ensure_root_is_owned ( ) ;
175174
176175 {
177- // Ideally we'd use the return of ensure_root_is_owned
178- // instead of re-unwrapping here but unfortunately that
179- // borrows all of out_tree and we need access to the
180- // length below.
181- let mut out_node = out_tree. root . as_mut ( ) . unwrap ( ) . push_level ( ) ;
176+ let out_root = BTreeMap :: ensure_is_owned ( & mut out_tree. root ) ;
177+ let mut out_node = out_root. push_level ( ) ;
182178 let mut in_edge = internal. first_edge ( ) ;
183179 while let Ok ( kv) = in_edge. right_kv ( ) {
184180 let ( k, v) = kv. into_kv ( ) ;
@@ -212,7 +208,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
212208 // Ord` constraint, which this method lacks.
213209 BTreeMap { root : None , length : 0 }
214210 } else {
215- clone_subtree ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) )
211+ clone_subtree ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) // unwrap succeeds because not empty
216212 }
217213 }
218214}
@@ -243,8 +239,8 @@ where
243239 }
244240
245241 fn replace ( & mut self , key : K ) -> Option < K > {
246- self . ensure_root_is_owned ( ) ;
247- match search:: search_tree :: < marker:: Mut < ' _ > , K , ( ) , K > ( self . root . as_mut ( ) ? . as_mut ( ) , & key) {
242+ let root = Self :: ensure_is_owned ( & mut self . root ) ;
243+ match search:: search_tree :: < marker:: Mut < ' _ > , K , ( ) , K > ( root. as_mut ( ) , & key) {
248244 Found ( handle) => Some ( mem:: replace ( handle. into_kv_mut ( ) . 0 , key) ) ,
249245 GoDown ( handle) => {
250246 VacantEntry { key, handle, length : & mut self . length , _marker : PhantomData }
@@ -943,7 +939,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
943939
944940 // Second, we build a tree from the sorted sequence in linear time.
945941 self . from_sorted_iter ( iter) ;
946- self . fix_right_edge ( ) ;
947942 }
948943
949944 /// Constructs a double-ended iterator over a sub-range of elements in the map.
@@ -1058,8 +1053,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
10581053 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
10591054 pub fn entry ( & mut self , key : K ) -> Entry < ' _ , K , V > {
10601055 // FIXME(@porglezomp) Avoid allocating if we don't insert
1061- self . ensure_root_is_owned ( ) ;
1062- match search:: search_tree ( self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) , & key) {
1056+ let root = Self :: ensure_is_owned ( & mut self . root ) ;
1057+ match search:: search_tree ( root. as_mut ( ) , & key) {
10631058 Found ( handle) => {
10641059 Occupied ( OccupiedEntry { handle, length : & mut self . length , _marker : PhantomData } )
10651060 }
@@ -1070,8 +1065,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
10701065 }
10711066
10721067 fn from_sorted_iter < I : Iterator < Item = ( K , V ) > > ( & mut self , iter : I ) {
1073- self . ensure_root_is_owned ( ) ;
1074- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) . last_leaf_edge ( ) . into_node ( ) ;
1068+ let root = Self :: ensure_is_owned ( & mut self . root ) ;
1069+ let mut cur_node = root. as_mut ( ) . last_leaf_edge ( ) . into_node ( ) ;
10751070 // Iterate through all key-value pairs, pushing them into nodes at the right level.
10761071 for ( key, value) in iter {
10771072 // Try to push key-value pair into the current leaf node.
@@ -1116,11 +1111,12 @@ impl<K: Ord, V> BTreeMap<K, V> {
11161111
11171112 self . length += 1 ;
11181113 }
1114+ Self :: fix_right_edge ( root)
11191115 }
11201116
1121- fn fix_right_edge ( & mut self ) {
1117+ fn fix_right_edge ( root : & mut node :: Root < K , V > ) {
11221118 // Handle underfull nodes, start from the top.
1123- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1119+ let mut cur_node = root. as_mut ( ) ;
11241120 while let Internal ( internal) = cur_node. force ( ) {
11251121 // Check if right-most child is underfull.
11261122 let mut last_edge = internal. last_edge ( ) ;
@@ -1179,16 +1175,17 @@ impl<K: Ord, V> BTreeMap<K, V> {
11791175 }
11801176
11811177 let total_num = self . len ( ) ;
1178+ let left_root = self . root . as_mut ( ) . unwrap ( ) ; // unwrap succeeds because not empty
11821179
11831180 let mut right = Self :: new ( ) ;
1184- let right_root = right. ensure_root_is_owned ( ) ;
1185- for _ in 0 ..( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) . height ( ) ) {
1181+ let right_root = Self :: ensure_is_owned ( & mut right. root ) ;
1182+ for _ in 0 ..left_root . height ( ) {
11861183 right_root. push_level ( ) ;
11871184 }
11881185
11891186 {
1190- let mut left_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1191- let mut right_node = right . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1187+ let mut left_node = left_root . as_mut ( ) ;
1188+ let mut right_node = right_root . as_mut ( ) ;
11921189
11931190 loop {
11941191 let mut split_edge = match search:: search_node ( left_node, key) {
@@ -1214,12 +1211,10 @@ impl<K: Ord, V> BTreeMap<K, V> {
12141211 }
12151212 }
12161213
1217- self . fix_right_border ( ) ;
1218- right . fix_left_border ( ) ;
1214+ Self :: fix_right_border ( left_root ) ;
1215+ Self :: fix_left_border ( right_root ) ;
12191216
1220- if self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) . height ( )
1221- < right. root . as_ref ( ) . unwrap ( ) . as_ref ( ) . height ( )
1222- {
1217+ if left_root. height ( ) < right_root. height ( ) {
12231218 self . recalc_length ( ) ;
12241219 right. length = total_num - self . len ( ) ;
12251220 } else {
@@ -1303,23 +1298,17 @@ impl<K: Ord, V> BTreeMap<K, V> {
13031298 }
13041299
13051300 /// Removes empty levels on the top.
1306- fn fix_top ( & mut self ) {
1307- loop {
1308- {
1309- let node = self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ;
1310- if node. height ( ) == 0 || node. len ( ) > 0 {
1311- break ;
1312- }
1313- }
1314- self . root . as_mut ( ) . unwrap ( ) . pop_level ( ) ;
1301+ fn fix_top ( root : & mut node:: Root < K , V > ) {
1302+ while root. height ( ) > 0 && root. as_ref ( ) . len ( ) == 0 {
1303+ root. pop_level ( ) ;
13151304 }
13161305 }
13171306
1318- fn fix_right_border ( & mut self ) {
1319- self . fix_top ( ) ;
1307+ fn fix_right_border ( root : & mut node :: Root < K , V > ) {
1308+ Self :: fix_top ( root ) ;
13201309
13211310 {
1322- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1311+ let mut cur_node = root. as_mut ( ) ;
13231312
13241313 while let Internal ( node) = cur_node. force ( ) {
13251314 let mut last_kv = node. last_kv ( ) ;
@@ -1337,15 +1326,15 @@ impl<K: Ord, V> BTreeMap<K, V> {
13371326 }
13381327 }
13391328
1340- self . fix_top ( ) ;
1329+ Self :: fix_top ( root ) ;
13411330 }
13421331
13431332 /// The symmetric clone of `fix_right_border`.
1344- fn fix_left_border ( & mut self ) {
1345- self . fix_top ( ) ;
1333+ fn fix_left_border ( root : & mut node :: Root < K , V > ) {
1334+ Self :: fix_top ( root ) ;
13461335
13471336 {
1348- let mut cur_node = self . root . as_mut ( ) . unwrap ( ) . as_mut ( ) ;
1337+ let mut cur_node = root. as_mut ( ) ;
13491338
13501339 while let Internal ( node) = cur_node. force ( ) {
13511340 let mut first_kv = node. first_kv ( ) ;
@@ -1362,7 +1351,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
13621351 }
13631352 }
13641353
1365- self . fix_top ( ) ;
1354+ Self :: fix_top ( root ) ;
13661355 }
13671356}
13681357
@@ -2321,9 +2310,9 @@ impl<K, V> BTreeMap<K, V> {
23212310 }
23222311
23232312 /// If the root node is the empty (non-allocated) root node, allocate our
2324- /// own node.
2325- fn ensure_root_is_owned ( & mut self ) -> & mut node:: Root < K , V > {
2326- self . root . get_or_insert_with ( node:: Root :: new_leaf)
2313+ /// own node. Is an associated function to avoid borrowing the entire BTreeMap.
2314+ fn ensure_is_owned ( root : & mut Option < node :: Root < K , V > > ) -> & mut node:: Root < K , V > {
2315+ root. get_or_insert_with ( node:: Root :: new_leaf)
23272316 }
23282317}
23292318
0 commit comments