@@ -571,11 +571,12 @@ impl<K, V, A: Allocator> BTreeMap<K, V, A> {
571
571
/// ```
572
572
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
573
573
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
+ } ) ;
579
580
}
580
581
581
582
/// 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> {
1594
1595
#[ stable( feature = "btree_drop" , since = "1.7.0" ) ]
1595
1596
impl < K , V , A : Allocator > Drop for IntoIter < K , V , A > {
1596
1597
fn drop ( & mut self ) {
1597
- self . dealloc ( )
1598
- }
1599
- }
1600
- impl < K , V , A : Allocator > IntoIter < K , V , A > {
1601
- fn dealloc ( & mut self ) {
1602
1598
struct DropGuard < ' a , K , V , A : Allocator > ( & ' a mut IntoIter < K , V , A > ) ;
1603
1599
1604
1600
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> {
1649
1645
Some ( unsafe { self . range . deallocating_next_back_unchecked ( & self . alloc ) } )
1650
1646
}
1651
1647
}
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
- }
1657
1648
}
1658
1649
1659
1650
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments