diff options
| author | C Jones <code@calebjones.net> | 2018-05-08 13:28:49 -0400 |
|---|---|---|
| committer | C Jones <code@calebjones.net> | 2018-05-08 13:28:49 -0400 |
| commit | e83c18f91d373592ecf7a0fbbc24d7597925af13 (patch) | |
| tree | d4a94d73d368dd7baadcc05b43fe9313cb92d43d /src/liballoc | |
| parent | f3a3599e090cb6aa63a327351738d7633c934728 (diff) | |
| download | rust-e83c18f91d373592ecf7a0fbbc24d7597925af13.tar.gz rust-e83c18f91d373592ecf7a0fbbc24d7597925af13.zip | |
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.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/btree/map.rs | 21 | ||||
| -rw-r--r-- | src/liballoc/btree/node.rs | 4 |
2 files changed, 10 insertions, 15 deletions
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<K, Q: ?Sized> super::Recover<Q> for BTreeMap<K, ()> } fn replace(&mut self, key: K) -> Option<K> { - if self.root.is_shared_root() { - self.root = node::Root::new_leaf(); - } + self.ensure_root_is_owned(); match search::search_tree::<marker::Mut, K, (), K>(self.root.as_mut(), &key) { Found(handle) => Some(mem::replace(handle.into_kv_mut().0, key)), GoDown(handle) => { @@ -893,10 +891,7 @@ impl<K: Ord, V> BTreeMap<K, V> { #[stable(feature = "rust1", since = "1.0.0")] pub fn entry(&mut self, key: K) -> Entry<K, V> { // 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<K: Ord, V> BTreeMap<K, V> { } fn from_sorted_iter<I: Iterator<Item = (K, V)>>(&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<K: Ord, V> BTreeMap<K, V> { 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<K, V> Root<K, V> { } 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( |
