From e83c18f91d373592ecf7a0fbbc24d7597925af13 Mon Sep 17 00:00:00 2001 From: C Jones Date: Tue, 8 May 2018 13:28:49 -0400 Subject: 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. --- src/liballoc/btree/map.rs | 21 ++++++++++----------- src/liballoc/btree/node.rs | 4 ---- 2 files changed, 10 insertions(+), 15 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/btree/map.rs b/src/liballoc/btree/map.rs index 7ed7fd204fd..bb2c68a27ba 100644 --- a/src/liballoc/btree/map.rs +++ b/src/liballoc/btree/map.rs @@ -246,9 +246,7 @@ impl super::Recover for BTreeMap } fn replace(&mut self, key: K) -> Option { - if self.root.is_shared_root() { - self.root = node::Root::new_leaf(); - } + self.ensure_root_is_owned(); match search::search_tree::(self.root.as_mut(), &key) { Found(handle) => Some(mem::replace(handle.into_kv_mut().0, key)), GoDown(handle) => { @@ -893,10 +891,7 @@ impl BTreeMap { #[stable(feature = "rust1", since = "1.0.0")] pub fn entry(&mut self, key: K) -> Entry { // FIXME(@porglezomp) Avoid allocating if we don't insert - if self.root.is_shared_root() { - self.root = node::Root::new_leaf(); - } - + self.ensure_root_is_owned(); match search::search_tree(self.root.as_mut(), &key) { Found(handle) => { Occupied(OccupiedEntry { @@ -917,10 +912,7 @@ impl BTreeMap { } fn from_sorted_iter>(&mut self, iter: I) { - if self.root.is_shared_root() { - self.root = node::Root::new_leaf(); - } - + self.ensure_root_is_owned(); let mut cur_node = last_leaf_edge(self.root.as_mut()).into_node(); // Iterate through all key-value pairs, pushing them into nodes at the right level. for (key, value) in iter { @@ -1165,6 +1157,13 @@ impl BTreeMap { self.fix_top(); } + + /// If the root node is the shared root node, allocate our own node. + fn ensure_root_is_owned(&mut self) { + if self.root.is_shared_root() { + self.root = node::Root::new_leaf(); + } + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/liballoc/btree/node.rs b/src/liballoc/btree/node.rs index 17eee65178e..431695c32ab 100644 --- a/src/liballoc/btree/node.rs +++ b/src/liballoc/btree/node.rs @@ -195,10 +195,6 @@ impl Root { } pub fn shared_empty_root() -> Self { - // Ensuring that the shared node hasn't been corrupted by any mutations - debug_assert!(EMPTY_ROOT_NODE.parent == ptr::null()); - debug_assert!(EMPTY_ROOT_NODE.parent_idx == 0); - debug_assert!(EMPTY_ROOT_NODE.len == 0); Root { node: unsafe { BoxedNode::from_ptr(NonNull::new_unchecked( -- cgit 1.4.1-3-g733a5