Skip to content

Commit e83c18f

Browse files
committed
Make an ensure_root_is_owned method to reduce duplication
Also remove some unnecessary debug_assert! when creating the shared root, since the root should be stored in the rodata and thus be impossible to accidentally modify.
1 parent f3a3599 commit e83c18f

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/liballoc/btree/map.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ impl<K, Q: ?Sized> super::Recover<Q> for BTreeMap<K, ()>
246246
}
247247

248248
fn replace(&mut self, key: K) -> Option<K> {
249-
if self.root.is_shared_root() {
250-
self.root = node::Root::new_leaf();
251-
}
249+
self.ensure_root_is_owned();
252250
match search::search_tree::<marker::Mut, K, (), K>(self.root.as_mut(), &key) {
253251
Found(handle) => Some(mem::replace(handle.into_kv_mut().0, key)),
254252
GoDown(handle) => {
@@ -893,10 +891,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
893891
#[stable(feature = "rust1", since = "1.0.0")]
894892
pub fn entry(&mut self, key: K) -> Entry<K, V> {
895893
// FIXME(@porglezomp) Avoid allocating if we don't insert
896-
if self.root.is_shared_root() {
897-
self.root = node::Root::new_leaf();
898-
}
899-
894+
self.ensure_root_is_owned();
900895
match search::search_tree(self.root.as_mut(), &key) {
901896
Found(handle) => {
902897
Occupied(OccupiedEntry {
@@ -917,10 +912,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
917912
}
918913

919914
fn from_sorted_iter<I: Iterator<Item = (K, V)>>(&mut self, iter: I) {
920-
if self.root.is_shared_root() {
921-
self.root = node::Root::new_leaf();
922-
}
923-
915+
self.ensure_root_is_owned();
924916
let mut cur_node = last_leaf_edge(self.root.as_mut()).into_node();
925917
// Iterate through all key-value pairs, pushing them into nodes at the right level.
926918
for (key, value) in iter {
@@ -1165,6 +1157,13 @@ impl<K: Ord, V> BTreeMap<K, V> {
11651157

11661158
self.fix_top();
11671159
}
1160+
1161+
/// If the root node is the shared root node, allocate our own node.
1162+
fn ensure_root_is_owned(&mut self) {
1163+
if self.root.is_shared_root() {
1164+
self.root = node::Root::new_leaf();
1165+
}
1166+
}
11681167
}
11691168

11701169
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/btree/node.rs

-4
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,6 @@ impl<K, V> Root<K, V> {
195195
}
196196

197197
pub fn shared_empty_root() -> Self {
198-
// Ensuring that the shared node hasn't been corrupted by any mutations
199-
debug_assert!(EMPTY_ROOT_NODE.parent == ptr::null());
200-
debug_assert!(EMPTY_ROOT_NODE.parent_idx == 0);
201-
debug_assert!(EMPTY_ROOT_NODE.len == 0);
202198
Root {
203199
node: unsafe {
204200
BoxedNode::from_ptr(NonNull::new_unchecked(

0 commit comments

Comments
 (0)