Skip to content

Commit

Permalink
btreemap-alloc: fix clear impl
Browse files Browse the repository at this point in the history
  • Loading branch information
exrook committed Jun 14, 2022
1 parent dc5951a commit 417b208
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,12 @@ impl<K, V, A: Allocator> BTreeMap<K, V, A> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self) {
let alloc = unsafe {
// drop all elements and retrieve allocator
ptr::read(self).into_iter().into_alloc()
};
*self = BTreeMap::new_in(alloc);
// avoid moving the allocator
mem::drop(BTreeMap {
root: mem::replace(&mut self.root, None),
length: mem::replace(&mut self.length, 0),
alloc: ManuallyDrop::new(&*self.alloc),
});
}

/// Makes a new empty BTreeMap with a reasonable choice for B.
Expand Down Expand Up @@ -1594,11 +1595,6 @@ impl<K, V, A: Allocator> IntoIterator for BTreeMap<K, V, A> {
#[stable(feature = "btree_drop", since = "1.7.0")]
impl<K, V, A: Allocator> Drop for IntoIter<K, V, A> {
fn drop(&mut self) {
self.dealloc()
}
}
impl<K, V, A: Allocator> IntoIter<K, V, A> {
fn dealloc(&mut self) {
struct DropGuard<'a, K, V, A: Allocator>(&'a mut IntoIter<K, V, A>);

impl<'a, K, V, A: Allocator> Drop for DropGuard<'a, K, V, A> {
Expand Down Expand Up @@ -1649,11 +1645,6 @@ impl<K, V, A: Allocator> IntoIter<K, V, A> {
Some(unsafe { self.range.deallocating_next_back_unchecked(&self.alloc) })
}
}
fn into_alloc(mut self) -> A {
self.dealloc(); // Deallocate, then don't drop as drop will also call dealloc
let iter = ManuallyDrop::new(self);
unsafe { ptr::read(&iter.alloc) }
}
}

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

0 comments on commit 417b208

Please sign in to comment.