@@ -46,7 +46,7 @@ pub struct HashTable<T, A = Global>
46
46
where
47
47
A : Allocator ,
48
48
{
49
- pub ( crate ) table : RawTable < T , A > ,
49
+ pub ( crate ) raw : RawTable < T , A > ,
50
50
}
51
51
52
52
impl < T > HashTable < T , Global > {
@@ -65,7 +65,7 @@ impl<T> HashTable<T, Global> {
65
65
/// ```
66
66
pub const fn new ( ) -> Self {
67
67
Self {
68
- table : RawTable :: new ( ) ,
68
+ raw : RawTable :: new ( ) ,
69
69
}
70
70
}
71
71
@@ -84,7 +84,7 @@ impl<T> HashTable<T, Global> {
84
84
/// ```
85
85
pub fn with_capacity ( capacity : usize ) -> Self {
86
86
Self {
87
- table : RawTable :: with_capacity ( capacity) ,
87
+ raw : RawTable :: with_capacity ( capacity) ,
88
88
}
89
89
}
90
90
}
@@ -133,7 +133,7 @@ where
133
133
/// ```
134
134
pub const fn new_in ( alloc : A ) -> Self {
135
135
Self {
136
- table : RawTable :: new_in ( alloc) ,
136
+ raw : RawTable :: new_in ( alloc) ,
137
137
}
138
138
}
139
139
@@ -182,13 +182,13 @@ where
182
182
/// ```
183
183
pub fn with_capacity_in ( capacity : usize , alloc : A ) -> Self {
184
184
Self {
185
- table : RawTable :: with_capacity_in ( capacity, alloc) ,
185
+ raw : RawTable :: with_capacity_in ( capacity, alloc) ,
186
186
}
187
187
}
188
188
189
189
/// Returns a reference to the underlying allocator.
190
190
pub fn allocator ( & self ) -> & A {
191
- self . table . allocator ( )
191
+ self . raw . allocator ( )
192
192
}
193
193
194
194
/// Returns a reference to an entry in the table with the given hash and
@@ -222,7 +222,7 @@ where
222
222
/// # }
223
223
/// ```
224
224
pub fn find ( & self , hash : u64 , eq : impl FnMut ( & T ) -> bool ) -> Option < & T > {
225
- self . table
225
+ self . raw
226
226
. find ( hash, eq)
227
227
. map ( |bucket| unsafe { bucket. as_ref ( ) } )
228
228
}
@@ -263,7 +263,7 @@ where
263
263
/// # }
264
264
/// ```
265
265
pub fn find_mut ( & mut self , hash : u64 , eq : impl FnMut ( & T ) -> bool ) -> Option < & mut T > {
266
- self . table
266
+ self . raw
267
267
. find ( hash, eq)
268
268
. map ( |bucket| unsafe { bucket. as_mut ( ) } )
269
269
}
@@ -292,7 +292,7 @@ where
292
292
/// let hasher = BuildHasherDefault::<AHasher>::default();
293
293
/// let hasher = |val: &_| hasher.hash_one(val);
294
294
/// table.insert_unchecked(hasher(&1), (1, "a"), |val| hasher(&val.0));
295
- /// if let Some (entry) = table.find_entry(hasher(&1), |val| val.0 == 1) {
295
+ /// if let Ok (entry) = table.find_entry(hasher(&1), |val| val.0 == 1) {
296
296
/// entry.remove();
297
297
/// }
298
298
/// assert_eq!(table.find(hasher(&1), |val| val.0 == 1), None);
@@ -306,12 +306,15 @@ where
306
306
& mut self ,
307
307
hash : u64 ,
308
308
eq : impl FnMut ( & T ) -> bool ,
309
- ) -> Option < OccupiedEntry < ' _ , T , A > > {
310
- self . table . find ( hash, eq) . map ( |bucket| OccupiedEntry {
311
- hash,
312
- bucket,
313
- table : & mut self . table ,
314
- } )
309
+ ) -> Result < OccupiedEntry < ' _ , T , A > , & mut Self > {
310
+ match self . raw . find ( hash, eq) {
311
+ Some ( bucket) => Ok ( OccupiedEntry {
312
+ hash,
313
+ bucket,
314
+ table : self ,
315
+ } ) ,
316
+ None => Err ( self ) ,
317
+ }
315
318
}
316
319
317
320
/// Returns an `Entry` for an entry in the table with the given hash
@@ -365,16 +368,16 @@ where
365
368
eq : impl FnMut ( & T ) -> bool ,
366
369
hasher : impl Fn ( & T ) -> u64 ,
367
370
) -> Entry < ' _ , T , A > {
368
- match self . table . find_or_find_insert_slot ( hash, eq, hasher) {
371
+ match self . raw . find_or_find_insert_slot ( hash, eq, hasher) {
369
372
Ok ( bucket) => Entry :: Occupied ( OccupiedEntry {
370
373
hash,
371
374
bucket,
372
- table : & mut self . table ,
375
+ table : self ,
373
376
} ) ,
374
377
Err ( insert_slot) => Entry :: Vacant ( VacantEntry {
375
378
hash,
376
379
insert_slot,
377
- table : & mut self . table ,
380
+ table : self ,
378
381
} ) ,
379
382
}
380
383
}
@@ -393,11 +396,11 @@ where
393
396
value : T ,
394
397
hasher : impl Fn ( & T ) -> u64 ,
395
398
) -> OccupiedEntry < ' _ , T , A > {
396
- let bucket = self . table . insert ( hash, value, hasher) ;
399
+ let bucket = self . raw . insert ( hash, value, hasher) ;
397
400
OccupiedEntry {
398
401
hash,
399
402
bucket,
400
- table : & mut self . table ,
403
+ table : self ,
401
404
}
402
405
}
403
406
@@ -425,7 +428,7 @@ where
425
428
/// # }
426
429
/// ```
427
430
pub fn clear ( & mut self ) {
428
- self . table . clear ( ) ;
431
+ self . raw . clear ( ) ;
429
432
}
430
433
431
434
/// Shrinks the capacity of the table as much as possible. It will drop
@@ -459,7 +462,7 @@ where
459
462
/// # }
460
463
/// ```
461
464
pub fn shrink_to_fit ( & mut self , hasher : impl Fn ( & T ) -> u64 ) {
462
- self . table . shrink_to ( self . len ( ) , hasher)
465
+ self . raw . shrink_to ( self . len ( ) , hasher)
463
466
}
464
467
465
468
/// Shrinks the capacity of the table with a lower limit. It will drop
@@ -498,7 +501,7 @@ where
498
501
/// # }
499
502
/// ```
500
503
pub fn shrink_to ( & mut self , min_capacity : usize , hasher : impl Fn ( & T ) -> u64 ) {
501
- self . table . shrink_to ( min_capacity, hasher) ;
504
+ self . raw . shrink_to ( min_capacity, hasher) ;
502
505
}
503
506
504
507
/// Reserves capacity for at least `additional` more elements to be inserted
@@ -538,7 +541,7 @@ where
538
541
/// # }
539
542
/// ```
540
543
pub fn reserve ( & mut self , additional : usize , hasher : impl Fn ( & T ) -> u64 ) {
541
- self . table . reserve ( additional, hasher)
544
+ self . raw . reserve ( additional, hasher)
542
545
}
543
546
544
547
/// Tries to reserve capacity for at least `additional` more elements to be inserted
@@ -579,7 +582,7 @@ where
579
582
additional : usize ,
580
583
hasher : impl Fn ( & T ) -> u64 ,
581
584
) -> Result < ( ) , TryReserveError > {
582
- self . table . try_reserve ( additional, hasher)
585
+ self . raw . try_reserve ( additional, hasher)
583
586
}
584
587
585
588
/// Returns the number of elements the table can hold without reallocating.
@@ -592,7 +595,7 @@ where
592
595
/// assert!(table.capacity() >= 100);
593
596
/// ```
594
597
pub fn capacity ( & self ) -> usize {
595
- self . table . capacity ( )
598
+ self . raw . capacity ( )
596
599
}
597
600
598
601
/// Returns the number of elements in the table.
@@ -619,7 +622,7 @@ where
619
622
/// # }
620
623
/// ```
621
624
pub fn len ( & self ) -> usize {
622
- self . table . len ( )
625
+ self . raw . len ( )
623
626
}
624
627
625
628
/// Returns `true` if the set contains no elements.
@@ -646,7 +649,7 @@ where
646
649
/// # }
647
650
/// ```
648
651
pub fn is_empty ( & self ) -> bool {
649
- self . table . is_empty ( )
652
+ self . raw . is_empty ( )
650
653
}
651
654
652
655
/// An iterator visiting all elements in arbitrary order.
@@ -679,7 +682,7 @@ where
679
682
/// ```
680
683
pub fn iter ( & self ) -> Iter < ' _ , T > {
681
684
Iter {
682
- inner : unsafe { self . table . iter ( ) } ,
685
+ inner : unsafe { self . raw . iter ( ) } ,
683
686
marker : PhantomData ,
684
687
}
685
688
}
@@ -731,7 +734,7 @@ where
731
734
/// ```
732
735
pub fn iter_mut ( & mut self ) -> IterMut < ' _ , T > {
733
736
IterMut {
734
- inner : unsafe { self . table . iter ( ) } ,
737
+ inner : unsafe { self . raw . iter ( ) } ,
735
738
marker : PhantomData ,
736
739
}
737
740
}
@@ -766,9 +769,9 @@ where
766
769
pub fn retain ( & mut self , mut f : impl FnMut ( & mut T ) -> bool ) {
767
770
// Here we only use `iter` as a temporary, preventing use-after-free
768
771
unsafe {
769
- for item in self . table . iter ( ) {
772
+ for item in self . raw . iter ( ) {
770
773
if !f ( item. as_mut ( ) ) {
771
- self . table . erase ( item) ;
774
+ self . raw . erase ( item) ;
772
775
}
773
776
}
774
777
}
@@ -807,7 +810,7 @@ where
807
810
/// ```
808
811
pub fn drain ( & mut self ) -> Drain < ' _ , T , A > {
809
812
Drain {
810
- inner : self . table . drain ( ) ,
813
+ inner : self . raw . drain ( ) ,
811
814
}
812
815
}
813
816
@@ -858,8 +861,8 @@ where
858
861
ExtractIf {
859
862
f,
860
863
inner : RawExtractIf {
861
- iter : unsafe { self . table . iter ( ) } ,
862
- table : & mut self . table ,
864
+ iter : unsafe { self . raw . iter ( ) } ,
865
+ table : & mut self . raw ,
863
866
} ,
864
867
}
865
868
}
@@ -922,7 +925,7 @@ where
922
925
hashes : [ u64 ; N ] ,
923
926
eq : impl FnMut ( usize , & T ) -> bool ,
924
927
) -> Option < [ & ' _ mut T ; N ] > {
925
- self . table . get_many_mut ( hashes, eq)
928
+ self . raw . get_many_mut ( hashes, eq)
926
929
}
927
930
928
931
/// Attempts to get mutable references to `N` values in the map at once, without validating that
@@ -992,7 +995,7 @@ where
992
995
hashes : [ u64 ; N ] ,
993
996
eq : impl FnMut ( usize , & T ) -> bool ,
994
997
) -> Option < [ & ' _ mut T ; N ] > {
995
- self . table . get_many_unchecked_mut ( hashes, eq)
998
+ self . raw . get_many_unchecked_mut ( hashes, eq)
996
999
}
997
1000
}
998
1001
@@ -1005,7 +1008,7 @@ where
1005
1008
1006
1009
fn into_iter ( self ) -> IntoIter < T , A > {
1007
1010
IntoIter {
1008
- inner : self . table . into_iter ( ) ,
1011
+ inner : self . raw . into_iter ( ) ,
1009
1012
}
1010
1013
}
1011
1014
}
@@ -1040,7 +1043,7 @@ where
1040
1043
{
1041
1044
fn default ( ) -> Self {
1042
1045
Self {
1043
- table : Default :: default ( ) ,
1046
+ raw : Default :: default ( ) ,
1044
1047
}
1045
1048
}
1046
1049
}
@@ -1052,7 +1055,7 @@ where
1052
1055
{
1053
1056
fn clone ( & self ) -> Self {
1054
1057
Self {
1055
- table : self . table . clone ( ) ,
1058
+ raw : self . raw . clone ( ) ,
1056
1059
}
1057
1060
}
1058
1061
}
@@ -1429,7 +1432,7 @@ where
1429
1432
{
1430
1433
hash : u64 ,
1431
1434
bucket : Bucket < T > ,
1432
- table : & ' a mut RawTable < T , A > ,
1435
+ table : & ' a mut HashTable < T , A > ,
1433
1436
}
1434
1437
1435
1438
unsafe impl < T , A > Send for OccupiedEntry < ' _ , T , A >
@@ -1496,7 +1499,7 @@ where
1496
1499
/// # }
1497
1500
/// ```
1498
1501
pub fn remove ( self ) -> ( T , VacantEntry < ' a , T , A > ) {
1499
- let ( val, slot) = unsafe { self . table . remove ( self . bucket ) } ;
1502
+ let ( val, slot) = unsafe { self . table . raw . remove ( self . bucket ) } ;
1500
1503
(
1501
1504
val,
1502
1505
VacantEntry {
@@ -1642,6 +1645,12 @@ where
1642
1645
pub fn into_mut ( self ) -> & ' a mut T {
1643
1646
unsafe { self . bucket . as_mut ( ) }
1644
1647
}
1648
+
1649
+ /// Converts the OccupiedEntry into a mutable reference to the underlying
1650
+ /// table.
1651
+ pub fn into_table ( self ) -> & ' a mut HashTable < T , A > {
1652
+ self . table
1653
+ }
1645
1654
}
1646
1655
1647
1656
/// A view into a vacant entry in a `HashTable`.
@@ -1689,7 +1698,7 @@ where
1689
1698
{
1690
1699
hash : u64 ,
1691
1700
insert_slot : InsertSlot ,
1692
- table : & ' a mut RawTable < T , A > ,
1701
+ table : & ' a mut HashTable < T , A > ,
1693
1702
}
1694
1703
1695
1704
impl < T : fmt:: Debug , A : Allocator > fmt:: Debug for VacantEntry < ' _ , T , A > {
@@ -1737,6 +1746,7 @@ where
1737
1746
pub fn insert ( self , value : T ) -> OccupiedEntry < ' a , T , A > {
1738
1747
let bucket = unsafe {
1739
1748
self . table
1749
+ . raw
1740
1750
. insert_in_slot ( self . hash , self . insert_slot , value)
1741
1751
} ;
1742
1752
OccupiedEntry {
@@ -1745,6 +1755,12 @@ where
1745
1755
table : self . table ,
1746
1756
}
1747
1757
}
1758
+
1759
+ /// Converts the OccupiedEntry into a mutable reference to the underlying
1760
+ /// table.
1761
+ pub fn into_table ( self ) -> & ' a mut HashTable < T , A > {
1762
+ self . table
1763
+ }
1748
1764
}
1749
1765
1750
1766
/// An iterator over the entries of a `HashTable` in arbitrary order.
0 commit comments