Skip to content

Commit 417b208

Browse files
committed
btreemap-alloc: fix clear impl
1 parent dc5951a commit 417b208

File tree

1 file changed

+6
-15
lines changed
  • library/alloc/src/collections/btree

1 file changed

+6
-15
lines changed

library/alloc/src/collections/btree/map.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,12 @@ impl<K, V, A: Allocator> BTreeMap<K, V, A> {
571571
/// ```
572572
#[stable(feature = "rust1", since = "1.0.0")]
573573
pub fn clear(&mut self) {
574-
let alloc = unsafe {
575-
// drop all elements and retrieve allocator
576-
ptr::read(self).into_iter().into_alloc()
577-
};
578-
*self = BTreeMap::new_in(alloc);
574+
// avoid moving the allocator
575+
mem::drop(BTreeMap {
576+
root: mem::replace(&mut self.root, None),
577+
length: mem::replace(&mut self.length, 0),
578+
alloc: ManuallyDrop::new(&*self.alloc),
579+
});
579580
}
580581

581582
/// Makes a new empty BTreeMap with a reasonable choice for B.
@@ -1594,11 +1595,6 @@ impl<K, V, A: Allocator> IntoIterator for BTreeMap<K, V, A> {
15941595
#[stable(feature = "btree_drop", since = "1.7.0")]
15951596
impl<K, V, A: Allocator> Drop for IntoIter<K, V, A> {
15961597
fn drop(&mut self) {
1597-
self.dealloc()
1598-
}
1599-
}
1600-
impl<K, V, A: Allocator> IntoIter<K, V, A> {
1601-
fn dealloc(&mut self) {
16021598
struct DropGuard<'a, K, V, A: Allocator>(&'a mut IntoIter<K, V, A>);
16031599

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

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

0 commit comments

Comments
 (0)