1
- use crate :: alloc:: alloc:: { Layout , handle_alloc_error } ;
1
+ use crate :: alloc:: alloc:: { handle_alloc_error , Layout } ;
2
2
use crate :: scopeguard:: guard;
3
3
use crate :: CollectionAllocErr ;
4
4
use core:: hint;
@@ -32,8 +32,8 @@ cfg_if! {
32
32
}
33
33
34
34
mod alloc;
35
- pub use self :: alloc:: Global ;
36
35
use self :: alloc:: Alloc ;
36
+ pub use self :: alloc:: Global ;
37
37
38
38
mod bitmask;
39
39
@@ -332,7 +332,7 @@ impl<T> Bucket<T> {
332
332
}
333
333
334
334
/// A raw hash table with an unsafe API.
335
- pub struct RawTable < A : Alloc + Copy , T > {
335
+ pub struct RawTable < T , A : Alloc + Clone > {
336
336
// Mask to get an index from a hash value. The value is one less than the
337
337
// number of buckets in the table.
338
338
bucket_mask : usize ,
@@ -355,16 +355,14 @@ pub struct RawTable<A: Alloc + Copy, T> {
355
355
alloc : A ,
356
356
}
357
357
358
- impl < A : Alloc + Copy , T > RawTable < A , T > {
358
+ impl < T , A : Alloc + Clone > RawTable < T , A > {
359
359
/// Creates a new empty hash table without allocating any memory.
360
360
///
361
361
/// In effect this returns a table with exactly 1 bucket. However we can
362
362
/// leave the data pointer dangling since that bucket is never written to
363
363
/// due to our load factor forcing us to always have at least 1 free bucket.
364
364
#[ cfg_attr( feature = "inline-more" , inline) ]
365
- pub fn new (
366
- alloc : A ,
367
- ) -> Self {
365
+ pub fn new ( alloc : A ) -> Self {
368
366
Self {
369
367
data : NonNull :: dangling ( ) ,
370
368
// Be careful to cast the entire slice to a raw pointer.
@@ -389,7 +387,8 @@ impl<A: Alloc + Copy, T> RawTable<A, T> {
389
387
debug_assert ! ( buckets. is_power_of_two( ) ) ;
390
388
let ( layout, data_offset) =
391
389
calculate_layout :: < T > ( buckets) . ok_or_else ( || fallability. capacity_overflow ( ) ) ?;
392
- let ctrl = alloc. alloc ( layout)
390
+ let ctrl = alloc
391
+ . alloc ( layout)
393
392
. map_err ( |_| fallability. alloc_err ( layout) ) ?;
394
393
let data = NonNull :: new_unchecked ( ctrl. as_ptr ( ) . add ( data_offset) as * mut T ) ;
395
394
Ok ( Self {
@@ -436,7 +435,8 @@ impl<A: Alloc + Copy, T> RawTable<A, T> {
436
435
unsafe fn free_buckets ( & mut self ) {
437
436
let ( layout, _) =
438
437
calculate_layout :: < T > ( self . buckets ( ) ) . unwrap_or_else ( || hint:: unreachable_unchecked ( ) ) ;
439
- self . alloc . dealloc ( NonNull :: new_unchecked ( self . ctrl . as_ptr ( ) ) , layout) ;
438
+ self . alloc
439
+ . dealloc ( NonNull :: new_unchecked ( self . ctrl . as_ptr ( ) ) , layout) ;
440
440
}
441
441
442
442
/// Returns the index of a bucket from a `Bucket`.
@@ -605,7 +605,7 @@ impl<A: Alloc + Copy, T> RawTable<A, T> {
605
605
// space for.
606
606
let min_size = usize:: max ( self . items , min_size) ;
607
607
if min_size == 0 {
608
- * self = Self :: new ( self . alloc ) ;
608
+ * self = Self :: new ( self . alloc . clone ( ) ) ;
609
609
return ;
610
610
}
611
611
@@ -622,7 +622,7 @@ impl<A: Alloc + Copy, T> RawTable<A, T> {
622
622
if min_buckets < self . buckets ( ) {
623
623
// Fast path if the table is empty
624
624
if self . items == 0 {
625
- * self = Self :: with_capacity ( self . alloc , min_size)
625
+ * self = Self :: with_capacity ( self . alloc . clone ( ) , min_size)
626
626
} else {
627
627
self . resize ( min_size, hasher, Fallibility :: Infallible )
628
628
. unwrap_or_else ( |_| unsafe { hint:: unreachable_unchecked ( ) } ) ;
@@ -797,7 +797,7 @@ impl<A: Alloc + Copy, T> RawTable<A, T> {
797
797
debug_assert ! ( self . items <= capacity) ;
798
798
799
799
// Allocate and initialize the new table.
800
- let mut new_table = Self :: try_with_capacity ( self . alloc , capacity, fallability) ?;
800
+ let mut new_table = Self :: try_with_capacity ( self . alloc . clone ( ) , capacity, fallability) ?;
801
801
new_table. growth_left -= self . items ;
802
802
new_table. items = self . items ;
803
803
@@ -962,10 +962,10 @@ impl<A: Alloc + Copy, T> RawTable<A, T> {
962
962
/// outlives the `RawDrain`. Because we cannot make the `next` method unsafe
963
963
/// on the `RawDrain`, we have to make the `drain` method unsafe.
964
964
#[ cfg_attr( feature = "inline-more" , inline) ]
965
- pub unsafe fn drain ( & mut self ) -> RawDrain < ' _ , A , T > {
965
+ pub unsafe fn drain ( & mut self ) -> RawDrain < ' _ , T , A > {
966
966
RawDrain {
967
967
iter : self . iter ( ) ,
968
- table : ManuallyDrop :: new ( mem:: replace ( self , Self :: new ( self . alloc ) ) ) ,
968
+ table : ManuallyDrop :: new ( mem:: replace ( self , Self :: new ( self . alloc . clone ( ) ) ) ) ,
969
969
orig_table : NonNull :: from ( self ) ,
970
970
marker : PhantomData ,
971
971
}
@@ -987,18 +987,22 @@ impl<A: Alloc + Copy, T> RawTable<A, T> {
987
987
}
988
988
}
989
989
990
- unsafe impl < A : Alloc + Copy , T > Send for RawTable < A , T > where T : Send { }
991
- unsafe impl < A : Alloc + Copy , T > Sync for RawTable < A , T > where T : Sync { }
990
+ unsafe impl < T , A : Alloc + Clone > Send for RawTable < T , A > where T : Send { }
991
+ unsafe impl < T , A : Alloc + Clone > Sync for RawTable < T , A > where T : Sync { }
992
992
993
- impl < A : Alloc + Copy , T : Clone > Clone for RawTable < A , T > {
993
+ impl < T : Clone , A : Alloc + Clone > Clone for RawTable < T , A > {
994
994
fn clone ( & self ) -> Self {
995
995
if self . is_empty_singleton ( ) {
996
- Self :: new ( self . alloc )
996
+ Self :: new ( self . alloc . clone ( ) )
997
997
} else {
998
998
unsafe {
999
999
let mut new_table = ManuallyDrop :: new (
1000
- Self :: new_uninitialized ( self . alloc , self . buckets ( ) , Fallibility :: Infallible )
1001
- . unwrap_or_else ( |_| hint:: unreachable_unchecked ( ) ) ,
1000
+ Self :: new_uninitialized (
1001
+ self . alloc . clone ( ) ,
1002
+ self . buckets ( ) ,
1003
+ Fallibility :: Infallible ,
1004
+ )
1005
+ . unwrap_or_else ( |_| hint:: unreachable_unchecked ( ) ) ,
1002
1006
) ;
1003
1007
1004
1008
// Copy the control bytes unchanged. We do this in a single pass
@@ -1043,7 +1047,7 @@ impl<A: Alloc + Copy, T: Clone> Clone for RawTable<A, T> {
1043
1047
}
1044
1048
1045
1049
#[ cfg( feature = "nightly" ) ]
1046
- unsafe impl < A : Alloc + Copy , # [ may_dangle ] T > Drop for RawTable < A , T > {
1050
+ unsafe impl < # [ may_dangle ] T , A : Alloc + Clone > Drop for RawTable < T , A > {
1047
1051
#[ cfg_attr( feature = "inline-more" , inline) ]
1048
1052
fn drop ( & mut self ) {
1049
1053
if !self . is_empty_singleton ( ) {
@@ -1059,7 +1063,7 @@ unsafe impl<A: Alloc + Copy, #[may_dangle] T> Drop for RawTable<A, T> {
1059
1063
}
1060
1064
}
1061
1065
#[ cfg( not( feature = "nightly" ) ) ]
1062
- impl < A : Alloc + Copy , T > Drop for RawTable < A , T > {
1066
+ impl < T , A : Alloc + Clone > Drop for RawTable < T , A > {
1063
1067
#[ cfg_attr( feature = "inline-more" , inline) ]
1064
1068
fn drop ( & mut self ) {
1065
1069
if !self . is_empty_singleton ( ) {
@@ -1075,14 +1079,14 @@ impl<A: Alloc + Copy, T> Drop for RawTable<A, T> {
1075
1079
}
1076
1080
}
1077
1081
1078
- impl < A : Alloc + Copy , T > IntoIterator for RawTable < A , T > {
1082
+ impl < T , A : Alloc + Clone > IntoIterator for RawTable < T , A > {
1079
1083
type Item = T ;
1080
- type IntoIter = RawIntoIter < A , T > ;
1084
+ type IntoIter = RawIntoIter < T , A > ;
1081
1085
1082
1086
#[ cfg_attr( feature = "inline-more" , inline) ]
1083
- fn into_iter ( self ) -> RawIntoIter < A , T > {
1087
+ fn into_iter ( self ) -> RawIntoIter < T , A > {
1084
1088
unsafe {
1085
- let allocator = self . alloc ;
1089
+ let allocator = self . alloc . clone ( ) ;
1086
1090
let iter = self . iter ( ) ;
1087
1091
let alloc = self . into_alloc ( ) ;
1088
1092
RawIntoIter {
@@ -1277,25 +1281,25 @@ impl<T> ExactSizeIterator for RawIter<T> {}
1277
1281
impl < T > FusedIterator for RawIter < T > { }
1278
1282
1279
1283
/// Iterator which consumes a table and returns elements.
1280
- pub struct RawIntoIter < A : Alloc + Copy , T > {
1284
+ pub struct RawIntoIter < T , A : Alloc + Clone > {
1281
1285
iter : RawIter < T > ,
1282
1286
alloc : Option < ( NonNull < u8 > , Layout ) > ,
1283
1287
marker : PhantomData < T > ,
1284
1288
allocator : A ,
1285
1289
}
1286
1290
1287
- impl < A : Alloc + Copy , T > RawIntoIter < A , T > {
1291
+ impl < T , A : Alloc + Clone > RawIntoIter < T , A > {
1288
1292
#[ cfg_attr( feature = "inline-more" , inline) ]
1289
1293
pub fn iter ( & self ) -> RawIter < T > {
1290
1294
self . iter . clone ( )
1291
1295
}
1292
1296
}
1293
1297
1294
- unsafe impl < A : Alloc + Copy , T > Send for RawIntoIter < A , T > where T : Send { }
1295
- unsafe impl < A : Alloc + Copy , T > Sync for RawIntoIter < A , T > where T : Sync { }
1298
+ unsafe impl < T , A : Alloc + Clone > Send for RawIntoIter < T , A > where T : Send { }
1299
+ unsafe impl < T , A : Alloc + Clone > Sync for RawIntoIter < T , A > where T : Sync { }
1296
1300
1297
1301
#[ cfg( feature = "nightly" ) ]
1298
- unsafe impl < A : Alloc + Copy , # [ may_dangle ] T > Drop for RawIntoIter < A , T > {
1302
+ unsafe impl < # [ may_dangle ] T , A : Alloc + Clone > Drop for RawIntoIter < T , A > {
1299
1303
#[ cfg_attr( feature = "inline-more" , inline) ]
1300
1304
fn drop ( & mut self ) {
1301
1305
unsafe {
@@ -1314,7 +1318,7 @@ unsafe impl<A: Alloc + Copy, #[may_dangle] T> Drop for RawIntoIter<A, T> {
1314
1318
}
1315
1319
}
1316
1320
#[ cfg( not( feature = "nightly" ) ) ]
1317
- impl < A : Alloc + Copy , T > Drop for RawIntoIter < A , T > {
1321
+ impl < T , A : Alloc + Clone > Drop for RawIntoIter < T , A > {
1318
1322
#[ cfg_attr( feature = "inline-more" , inline) ]
1319
1323
fn drop ( & mut self ) {
1320
1324
unsafe {
@@ -1327,13 +1331,14 @@ impl<A: Alloc + Copy, T> Drop for RawIntoIter<A, T> {
1327
1331
1328
1332
// Free the table
1329
1333
if let Some ( ( ptr, layout) ) = self . alloc {
1330
- self . allocator . dealloc ( NonNull :: new_unchecked ( ptr. as_ptr ( ) ) , layout) ;
1334
+ self . allocator
1335
+ . dealloc ( NonNull :: new_unchecked ( ptr. as_ptr ( ) ) , layout) ;
1331
1336
}
1332
1337
}
1333
1338
}
1334
1339
}
1335
1340
1336
- impl < A : Alloc + Copy , T > Iterator for RawIntoIter < A , T > {
1341
+ impl < T , A : Alloc + Clone > Iterator for RawIntoIter < T , A > {
1337
1342
type Item = T ;
1338
1343
1339
1344
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -1347,35 +1352,35 @@ impl<A: Alloc+ Copy, T> Iterator for RawIntoIter<A, T> {
1347
1352
}
1348
1353
}
1349
1354
1350
- impl < A : Alloc + Copy , T > ExactSizeIterator for RawIntoIter < A , T > { }
1351
- impl < A : Alloc + Copy , T > FusedIterator for RawIntoIter < A , T > { }
1355
+ impl < T , A : Alloc + Clone > ExactSizeIterator for RawIntoIter < T , A > { }
1356
+ impl < T , A : Alloc + Clone > FusedIterator for RawIntoIter < T , A > { }
1352
1357
1353
1358
/// Iterator which consumes elements without freeing the table storage.
1354
- pub struct RawDrain < ' a , A : Alloc + Copy , T > {
1359
+ pub struct RawDrain < ' a , T , A : Alloc + Clone > {
1355
1360
iter : RawIter < T > ,
1356
1361
1357
1362
// The table is moved into the iterator for the duration of the drain. This
1358
1363
// ensures that an empty table is left if the drain iterator is leaked
1359
1364
// without dropping.
1360
- table : ManuallyDrop < RawTable < A , T > > ,
1361
- orig_table : NonNull < RawTable < A , T > > ,
1365
+ table : ManuallyDrop < RawTable < T , A > > ,
1366
+ orig_table : NonNull < RawTable < T , A > > ,
1362
1367
1363
1368
// We don't use a &'a mut RawTable<T> because we want RawDrain to be
1364
1369
// covariant over T.
1365
- marker : PhantomData < & ' a RawTable < A , T > > ,
1370
+ marker : PhantomData < & ' a RawTable < T , A > > ,
1366
1371
}
1367
1372
1368
- impl < A : Alloc + Copy , T > RawDrain < ' _ , A , T > {
1373
+ impl < T , A : Alloc + Clone > RawDrain < ' _ , T , A > {
1369
1374
#[ cfg_attr( feature = "inline-more" , inline) ]
1370
- pub fn iter ( & self ) -> RawIter < T > {
1375
+ pub fn iter ( & self ) -> RawIter < T > {
1371
1376
self . iter . clone ( )
1372
1377
}
1373
1378
}
1374
1379
1375
- unsafe impl < A : Alloc + Copy , T > Send for RawDrain < ' _ , A , T > where T : Send { }
1376
- unsafe impl < A : Alloc + Copy , T > Sync for RawDrain < ' _ , A , T > where T : Sync { }
1380
+ unsafe impl < T , A : Alloc + Copy > Send for RawDrain < ' _ , T , A > where T : Send { }
1381
+ unsafe impl < T , A : Alloc + Copy > Sync for RawDrain < ' _ , T , A > where T : Sync { }
1377
1382
1378
- impl < A : Alloc + Copy , T > Drop for RawDrain < ' _ , A , T > {
1383
+ impl < T , A : Alloc + Clone > Drop for RawDrain < ' _ , T , A > {
1379
1384
#[ cfg_attr( feature = "inline-more" , inline) ]
1380
1385
fn drop ( & mut self ) {
1381
1386
unsafe {
@@ -1398,7 +1403,7 @@ impl<A: Alloc + Copy, T> Drop for RawDrain<'_, A, T> {
1398
1403
}
1399
1404
}
1400
1405
1401
- impl < A : Alloc + Copy , T > Iterator for RawDrain < ' _ , A , T > {
1406
+ impl < T , A : Alloc + Clone > Iterator for RawDrain < ' _ , T , A > {
1402
1407
type Item = T ;
1403
1408
1404
1409
#[ cfg_attr( feature = "inline-more" , inline) ]
@@ -1415,5 +1420,5 @@ impl<A: Alloc + Copy, T> Iterator for RawDrain<'_, A, T> {
1415
1420
}
1416
1421
}
1417
1422
1418
- impl < A : Alloc + Copy , T > ExactSizeIterator for RawDrain < ' _ , A , T > { }
1419
- impl < A : Alloc + Copy , T > FusedIterator for RawDrain < ' _ , A , T > { }
1423
+ impl < T , A : Alloc + Clone > ExactSizeIterator for RawDrain < ' _ , T , A > { }
1424
+ impl < T , A : Alloc + Clone > FusedIterator for RawDrain < ' _ , T , A > { }
0 commit comments