@@ -216,7 +216,9 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
216216 match node. force ( ) {
217217 Leaf ( leaf) => {
218218 let mut out_tree = BTreeMap {
219- root : Some ( Root :: new ( alloc. clone ( ) ) ) ,
219+ // SAFETY: `alloc` is the allocator for both the original and the cloned
220+ // `BTreeMap`.
221+ root : unsafe { Some ( Root :: new ( alloc. clone ( ) ) ) } ,
220222 length : 0 ,
221223 alloc : ManuallyDrop :: new ( alloc) ,
222224 _marker : PhantomData ,
@@ -247,7 +249,9 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
247249
248250 {
249251 let out_root = out_tree. root . as_mut ( ) . unwrap ( ) ;
250- let mut out_node = out_root. push_internal_level ( alloc. clone ( ) ) ;
252+ // SAFETY: `alloc` is the allocator for both the original and the cloned
253+ // `BTreeMap`.
254+ let mut out_node = unsafe { out_root. push_internal_level ( alloc. clone ( ) ) } ;
251255 let mut in_edge = internal. first_edge ( ) ;
252256 while let Ok ( kv) = in_edge. right_kv ( ) {
253257 let ( k, v) = kv. into_kv ( ) ;
@@ -269,7 +273,9 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
269273 out_node. push (
270274 k,
271275 v,
272- subroot. unwrap_or_else ( || Root :: new ( alloc. clone ( ) ) ) ,
276+ // SAFETY: `alloc` is the allocator for both the original and cloned
277+ // `BTreeMap`.
278+ subroot. unwrap_or_else ( || unsafe { Root :: new ( alloc. clone ( ) ) } ) ,
273279 ) ;
274280 out_tree. length += 1 + sublength;
275281 }
@@ -323,8 +329,9 @@ where
323329
324330 fn replace ( & mut self , key : K ) -> Option < K > {
325331 let ( map, dormant_map) = DormantMutRef :: new ( self ) ;
332+ // SAFETY: `alloc` is the allocator for the `BTreeMap`.
326333 let root_node =
327- map. root . get_or_insert_with ( || Root :: new ( ( * map. alloc ) . clone ( ) ) ) . borrow_mut ( ) ;
334+ map. root . get_or_insert_with ( || unsafe { Root :: new ( ( * map. alloc ) . clone ( ) ) } ) . borrow_mut ( ) ;
328335 match root_node. search_tree :: < K > ( & key) {
329336 Found ( mut kv) => Some ( mem:: replace ( kv. key_mut ( ) , key) ) ,
330337 GoDown ( handle) => {
@@ -1144,13 +1151,16 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11441151
11451152 let self_iter = mem:: replace ( self , Self :: new_in ( ( * self . alloc ) . clone ( ) ) ) . into_iter ( ) ;
11461153 let other_iter = mem:: replace ( other, Self :: new_in ( ( * self . alloc ) . clone ( ) ) ) . into_iter ( ) ;
1147- let root = self . root . get_or_insert_with ( || Root :: new ( ( * self . alloc ) . clone ( ) ) ) ;
1148- root. append_from_sorted_iters (
1149- self_iter,
1150- other_iter,
1151- & mut self . length ,
1152- ( * self . alloc ) . clone ( ) ,
1153- )
1154+ let root = self . root . get_or_insert_with ( || unsafe { Root :: new ( ( * self . alloc ) . clone ( ) ) } ) ;
1155+ // SAFETY: `self.alloc` is the allocator for the `BTreeMap`.
1156+ unsafe {
1157+ root. append_from_sorted_iters (
1158+ self_iter,
1159+ other_iter,
1160+ & mut self . length ,
1161+ ( * self . alloc ) . clone ( ) ,
1162+ )
1163+ }
11541164 }
11551165
11561166 /// Constructs a double-ended iterator over a sub-range of elements in the map.
@@ -1464,9 +1474,13 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14641474 K : Ord ,
14651475 I : IntoIterator < Item = ( K , V ) > ,
14661476 {
1467- let mut root = Root :: new ( alloc. clone ( ) ) ;
1477+ // SAFETY: `alloc` is the allocator for the returned `BTreeMap`.
1478+ let mut root = unsafe { Root :: new ( alloc. clone ( ) ) } ;
14681479 let mut length = 0 ;
1469- root. bulk_push ( DedupSortedIter :: new ( iter. into_iter ( ) ) , & mut length, alloc. clone ( ) ) ;
1480+ // SAFETY: `alloc` is the allocator for the returned `BTreeMap`.
1481+ unsafe {
1482+ root. bulk_push ( DedupSortedIter :: new ( iter. into_iter ( ) ) , & mut length, alloc. clone ( ) ) ;
1483+ }
14701484 BTreeMap { root : Some ( root) , length, alloc : ManuallyDrop :: new ( alloc) , _marker : PhantomData }
14711485 }
14721486}
0 commit comments