@@ -216,7 +216,9 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
216
216
match node. force ( ) {
217
217
Leaf ( leaf) => {
218
218
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 ( ) ) ) } ,
220
222
length : 0 ,
221
223
alloc : ManuallyDrop :: new ( alloc) ,
222
224
_marker : PhantomData ,
@@ -247,7 +249,9 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
247
249
248
250
{
249
251
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 ( ) ) } ;
251
255
let mut in_edge = internal. first_edge ( ) ;
252
256
while let Ok ( kv) = in_edge. right_kv ( ) {
253
257
let ( k, v) = kv. into_kv ( ) ;
@@ -269,7 +273,9 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
269
273
out_node. push (
270
274
k,
271
275
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 ( ) ) } ) ,
273
279
) ;
274
280
out_tree. length += 1 + sublength;
275
281
}
@@ -323,8 +329,9 @@ where
323
329
324
330
fn replace ( & mut self , key : K ) -> Option < K > {
325
331
let ( map, dormant_map) = DormantMutRef :: new ( self ) ;
332
+ // SAFETY: `alloc` is the allocator for the `BTreeMap`.
326
333
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 ( ) ;
328
335
match root_node. search_tree :: < K > ( & key) {
329
336
Found ( mut kv) => Some ( mem:: replace ( kv. key_mut ( ) , key) ) ,
330
337
GoDown ( handle) => {
@@ -1144,13 +1151,16 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
1144
1151
1145
1152
let self_iter = mem:: replace ( self , Self :: new_in ( ( * self . alloc ) . clone ( ) ) ) . into_iter ( ) ;
1146
1153
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
+ }
1154
1164
}
1155
1165
1156
1166
/// 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> {
1464
1474
K : Ord ,
1465
1475
I : IntoIterator < Item = ( K , V ) > ,
1466
1476
{
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 ( ) ) } ;
1468
1479
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
+ }
1470
1484
BTreeMap { root : Some ( root) , length, alloc : ManuallyDrop :: new ( alloc) , _marker : PhantomData }
1471
1485
}
1472
1486
}
0 commit comments