Don't drop the shared static node

We modify the drop implementation in IntoIter to not drop the shared root
This commit is contained in:
C Jones 2018-05-01 00:21:30 -04:00
parent ef6060c863
commit fa62eba92a
2 changed files with 13 additions and 8 deletions

View file

@ -686,10 +686,6 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
if self.root.is_shared_root() {
self.root = node::Root::new_leaf();
}
match self.entry(key) {
Occupied(mut entry) => Some(entry.insert(value)),
Vacant(entry) => {
@ -1301,6 +1297,10 @@ impl<K, V> Drop for IntoIter<K, V> {
self.for_each(drop);
unsafe {
let leaf_node = ptr::read(&self.front).into_node();
if leaf_node.is_shared_root() {
return;
}
if let Some(first_parent) = leaf_node.deallocate_and_ascend() {
let mut cur_node = first_parent.into_node();
while let Some(parent) = cur_node.deallocate_and_ascend() {

View file

@ -101,6 +101,10 @@ impl<K, V> LeafNode<K, V> {
len: 0
}
}
fn is_shared_root(&self) -> bool {
self as *const _ == &EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V>
}
}
// We need to implement Sync here in order to make a static instance
@ -185,10 +189,7 @@ unsafe impl<K: Send, V: Send> Send for Root<K, V> { }
impl<K, V> Root<K, V> {
pub fn is_shared_root(&self) -> bool {
ptr::eq(
self.node.as_ptr().as_ptr(),
&EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V>,
)
self.as_ref().is_shared_root()
}
pub fn shared_empty_root() -> Self {
@ -387,6 +388,10 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
}
}
pub fn is_shared_root(&self) -> bool {
self.as_leaf().is_shared_root()
}
pub fn keys(&self) -> &[K] {
self.reborrow().into_slices().0
}