1
1
use core:: borrow:: Borrow ;
2
2
use core:: hash:: { BuildHasher , Hash } ;
3
+ use core:: marker:: PhantomData ;
3
4
use core:: ops:: Index ;
4
5
use core:: { fmt, mem} ;
5
6
@@ -8,26 +9,30 @@ use crate::errors::{CapacityError, RescaleError, UnavailableMutError};
8
9
use crate :: ext:: { TryExtend , TryFromIterator } ;
9
10
use crate :: iter:: { Drain , DrainFilter , Iter , IterMut , Keys , Values , ValuesMut } ;
10
11
use crate :: occupied:: OccupiedEntry ;
11
- use crate :: raw:: { ArrayTable , RawEntryBuilder , RawTable } ;
12
+ use crate :: raw:: { ArrayTable , RawEntryBuilder , RawTable , RawTableIter } ;
12
13
use crate :: utils;
13
14
use crate :: vacant:: VacantEntry ;
14
15
15
- /// Default hasher for [`ArrayMap `].
16
+ /// Default hasher for [`ArrayMapFacade `].
16
17
#[ cfg( feature = "ahash" ) ]
17
18
pub type DefaultHashBuilder = core:: hash:: BuildHasherDefault < ahash:: AHasher > ;
18
19
/// Dummy default hasher
19
20
#[ cfg( not( feature = "ahash" ) ) ]
20
21
pub enum DefaultHashBuilder { }
21
22
23
+ pub type ArrayMap < K , V , const N : usize , B = DefaultHashBuilder > =
24
+ ArrayMapFacade < K , V , ArrayTable < ( K , V ) , N > , B > ;
25
+
22
26
#[ derive( Copy , Clone ) ]
23
- pub struct ArrayMap < K , V , const N : usize , B = DefaultHashBuilder > {
24
- table : ArrayTable < ( K , V ) , N > ,
27
+ pub struct ArrayMapFacade < K , V , R : RawTable < ( K , V ) > , B = DefaultHashBuilder > {
28
+ table : R ,
25
29
build_hasher : B ,
30
+ _p : PhantomData < ( K , V ) > ,
26
31
}
27
32
28
33
#[ cfg( feature = "ahash" ) ]
29
- impl < K , V , const N : usize > ArrayMap < K , V , N , DefaultHashBuilder > {
30
- /// Creates an empty [`ArrayMap `] with the [`DefaultHashBuilder`].
34
+ impl < K , V , R : RawTable < ( K , V ) > + Default > ArrayMapFacade < K , V , R , DefaultHashBuilder > {
35
+ /// Creates an empty [`ArrayMapFacade `] with the [`DefaultHashBuilder`].
31
36
///
32
37
/// # Examples
33
38
///
@@ -42,8 +47,8 @@ impl<K, V, const N: usize> ArrayMap<K, V, N, DefaultHashBuilder> {
42
47
}
43
48
}
44
49
45
- impl < K , V , const N : usize , B : BuildHasher > ArrayMap < K , V , N , B > {
46
- /// Creates an empty [`ArrayMap `] with the provided [`BuildHasher`].
50
+ impl < K , V , R : RawTable < ( K , V ) > + Default , B : BuildHasher > ArrayMapFacade < K , V , R , B > {
51
+ /// Creates an empty [`ArrayMapFacade `] with the provided [`BuildHasher`].
47
52
///
48
53
/// # Note
49
54
///
@@ -66,7 +71,7 @@ impl<K, V, const N: usize, B: BuildHasher> ArrayMap<K, V, N, B> {
66
71
Self :: with_build_hasher ( build_hasher)
67
72
}
68
73
69
- /// Creates an empty [`ArrayMap `] with the provided [`BuildHasher`].
74
+ /// Creates an empty [`ArrayMapFacade `] with the provided [`BuildHasher`].
70
75
///
71
76
/// # Examples
72
77
///
@@ -81,11 +86,14 @@ impl<K, V, const N: usize, B: BuildHasher> ArrayMap<K, V, N, B> {
81
86
#[ doc( alias( "with_hasher" ) ) ]
82
87
pub fn with_build_hasher ( build_hasher : B ) -> Self {
83
88
Self {
84
- table : ArrayTable :: default ( ) ,
89
+ table : R :: default ( ) ,
85
90
build_hasher,
91
+ _p : PhantomData ,
86
92
}
87
93
}
94
+ }
88
95
96
+ impl < K , V , R : RawTable < ( K , V ) > , B : BuildHasher > ArrayMapFacade < K , V , R , B > {
89
97
/// Returns the number of elements the map can hold in total.
90
98
///
91
99
/// The returned value, will be equal to the const generic `N`.
@@ -103,7 +111,7 @@ impl<K, V, const N: usize, B: BuildHasher> ArrayMap<K, V, N, B> {
103
111
/// ```
104
112
#[ must_use]
105
113
pub fn capacity ( & self ) -> usize {
106
- N
114
+ self . table . capacity ( )
107
115
}
108
116
109
117
/// Returns the number of elements in the map.
@@ -164,9 +172,10 @@ impl<K, V, const N: usize, B: BuildHasher> ArrayMap<K, V, N, B> {
164
172
}
165
173
}
166
174
167
- impl < K , V , B , const N : usize > ArrayMap < K , V , N , B >
175
+ impl < K , V , R , B > ArrayMapFacade < K , V , R , B >
168
176
where
169
177
K : Eq + Hash ,
178
+ R : RawTable < ( K , V ) > ,
170
179
B : BuildHasher ,
171
180
{
172
181
/// Gets the given key's corresponding entry in the map for in-place
@@ -192,10 +201,7 @@ where
192
201
/// assert_eq!(letters.get(&'y'), None);
193
202
/// # Ok::<_, array_map::CapacityError>(())
194
203
/// ```
195
- pub fn entry (
196
- & mut self ,
197
- key : K ,
198
- ) -> Result < Entry < ' _ , K , V , ArrayTable < ( K , V ) , N > , B > , CapacityError > {
204
+ pub fn entry ( & mut self , key : K ) -> Result < Entry < ' _ , K , V , R , B > , CapacityError > {
199
205
let hash = utils:: make_hash :: < K , K , B > ( & self . build_hasher , & key) ;
200
206
201
207
if let Some ( ident) = self . table . find ( hash, |( k, _) | k. eq ( & key) ) {
@@ -639,7 +645,7 @@ where
639
645
///
640
646
/// assert_eq!(drained, [None, None, None, Some(("rust", "rost")),]);
641
647
/// ```
642
- pub fn drain_filter < F > ( & mut self , f : F ) -> DrainFilter < ' _ , K , V , F , ArrayTable < ( K , V ) , N > , B >
648
+ pub fn drain_filter < F > ( & mut self , f : F ) -> DrainFilter < ' _ , K , V , F , R , B >
643
649
where
644
650
F : FnMut ( & K , & mut V ) -> bool ,
645
651
{
@@ -672,7 +678,7 @@ where
672
678
/// ]
673
679
/// );
674
680
/// ```
675
- pub fn drain ( & mut self ) -> Drain < ' _ , K , V , ArrayTable < ( K , V ) , N > , B > {
681
+ pub fn drain ( & mut self ) -> Drain < ' _ , K , V , R , B > {
676
682
Drain :: new ( & mut self . table , & self . build_hasher )
677
683
}
678
684
@@ -701,11 +707,9 @@ where
701
707
/// assert_eq!(rescaled.get(&'a'), Some(&('a' as u32)));
702
708
/// # Ok::<_, array_map::CapacityError>(())
703
709
/// ```
704
- pub fn try_rescale < const M : usize > (
705
- mut self ,
706
- ) -> Result < ArrayMap < K , V , M , B > , RescaleError < N , M > > {
710
+ pub fn try_rescale < const M : usize > ( mut self ) -> Result < ArrayMap < K , V , M , B > , RescaleError < M > > {
707
711
if self . len ( ) >= M {
708
- return Err ( RescaleError :: new ( self . len ( ) ) ) ;
712
+ return Err ( RescaleError :: new ( self . len ( ) , self . capacity ( ) ) ) ;
709
713
}
710
714
711
715
let mut result = ArrayMap :: with_build_hasher ( self . build_hasher ) ;
@@ -778,16 +782,20 @@ where
778
782
/// Immutable raw entries have a very limited use; you might instead want to
779
783
/// use `ArrayMap::raw_entry_mut`.
780
784
#[ must_use]
781
- pub fn raw_entry ( & self ) -> RawEntryBuilder < ' _ , K , V , ArrayTable < ( K , V ) , N > , B > {
785
+ pub fn raw_entry ( & self ) -> RawEntryBuilder < ' _ , K , V , R , B > {
782
786
RawEntryBuilder :: new ( & self . table , & self . build_hasher )
783
787
}
784
788
785
- pub ( crate ) fn into_parts ( self ) -> ( B , <ArrayTable < ( K , V ) , N > as IntoIterator >:: IntoIter ) {
789
+ pub ( crate ) fn into_parts ( self ) -> ( B , <R as IntoIterator >:: IntoIter ) {
786
790
( self . build_hasher , self . table . into_iter ( ) )
787
791
}
788
792
}
789
793
790
- impl < K , V , B : BuildHasher , const N : usize > ArrayMap < K , V , N , B > {
794
+ impl < K , V , R , B > ArrayMapFacade < K , V , R , B >
795
+ where
796
+ R : RawTableIter < ( K , V ) > ,
797
+ B : BuildHasher ,
798
+ {
791
799
/// Returns an iterator iterating over the immutable entries of the map.
792
800
///
793
801
/// # Examples
@@ -816,7 +824,7 @@ impl<K, V, B: BuildHasher, const N: usize> ArrayMap<K, V, N, B> {
816
824
/// );
817
825
/// # Ok::<_, CollectArrayError>(())
818
826
/// ```
819
- pub fn iter ( & self ) -> Iter < ' _ , K , V , ArrayTable < ( K , V ) , N > > {
827
+ pub fn iter ( & self ) -> Iter < ' _ , K , V , R > {
820
828
Iter :: new ( & self . table )
821
829
}
822
830
@@ -850,7 +858,7 @@ impl<K, V, B: BuildHasher, const N: usize> ArrayMap<K, V, N, B> {
850
858
/// }
851
859
/// );
852
860
/// ```
853
- pub fn iter_mut ( & mut self ) -> IterMut < ' _ , K , V , ArrayTable < ( K , V ) , N > > {
861
+ pub fn iter_mut ( & mut self ) -> IterMut < ' _ , K , V , R > {
854
862
IterMut :: new ( & mut self . table )
855
863
}
856
864
@@ -876,7 +884,7 @@ impl<K, V, B: BuildHasher, const N: usize> ArrayMap<K, V, N, B> {
876
884
/// assert_eq!(keys, [&"good bye", &"good night", &"hello",]);
877
885
/// # Ok::<_, CollectArrayError>(())
878
886
/// ```
879
- pub fn keys ( & self ) -> Keys < ' _ , K , V , ArrayTable < ( K , V ) , N > > {
887
+ pub fn keys ( & self ) -> Keys < ' _ , K , V , R > {
880
888
Keys :: new ( self . iter ( ) )
881
889
}
882
890
@@ -902,7 +910,7 @@ impl<K, V, B: BuildHasher, const N: usize> ArrayMap<K, V, N, B> {
902
910
/// assert_eq!(values, [&"au revoir", &"bonne nuit", &"salut",]);
903
911
/// # Ok::<_, CollectArrayError>(())
904
912
/// ```
905
- pub fn values ( & self ) -> Values < ' _ , K , V , ArrayTable < ( K , V ) , N > > {
913
+ pub fn values ( & self ) -> Values < ' _ , K , V , R > {
906
914
Values :: new ( self . iter ( ) )
907
915
}
908
916
@@ -937,15 +945,16 @@ impl<K, V, B: BuildHasher, const N: usize> ArrayMap<K, V, N, B> {
937
945
/// }
938
946
/// );
939
947
/// ```
940
- pub fn values_mut ( & mut self ) -> ValuesMut < ' _ , K , V , ArrayTable < ( K , V ) , N > > {
948
+ pub fn values_mut ( & mut self ) -> ValuesMut < ' _ , K , V , R > {
941
949
ValuesMut :: new ( self . iter_mut ( ) )
942
950
}
943
951
}
944
952
945
- impl < K , Q : ?Sized , V , B , const N : usize > Index < & Q > for ArrayMap < K , V , N , B >
953
+ impl < K , Q : ?Sized , V , R , B > Index < & Q > for ArrayMapFacade < K , V , R , B >
946
954
where
947
955
K : Eq + Hash + Borrow < Q > ,
948
956
Q : Eq + Hash ,
957
+ R : RawTable < ( K , V ) > ,
949
958
B : BuildHasher ,
950
959
{
951
960
type Output = V ;
@@ -960,53 +969,72 @@ where
960
969
}
961
970
}
962
971
963
- impl < ' a , K , V , B : BuildHasher , const N : usize > IntoIterator for & ' a ArrayMap < K , V , N , B > {
964
- type IntoIter = Iter < ' a , K , V , ArrayTable < ( K , V ) , N > > ;
972
+ impl < ' a , K , V , R , B > IntoIterator for & ' a ArrayMapFacade < K , V , R , B >
973
+ where
974
+ R : RawTableIter < ( K , V ) > ,
975
+ B : BuildHasher ,
976
+ {
977
+ type IntoIter = Iter < ' a , K , V , R > ;
965
978
type Item = ( & ' a K , & ' a V ) ;
966
979
967
980
fn into_iter ( self ) -> Self :: IntoIter {
968
981
self . iter ( )
969
982
}
970
983
}
971
984
972
- impl < ' a , K , V , B : BuildHasher , const N : usize > IntoIterator for & ' a mut ArrayMap < K , V , N , B > {
973
- type IntoIter = IterMut < ' a , K , V , ArrayTable < ( K , V ) , N > > ;
985
+ impl < ' a , K , V , R , B > IntoIterator for & ' a mut ArrayMapFacade < K , V , R , B >
986
+ where
987
+ R : RawTableIter < ( K , V ) > ,
988
+ B : BuildHasher ,
989
+ {
990
+ type IntoIter = IterMut < ' a , K , V , R > ;
974
991
type Item = ( & ' a K , & ' a mut V ) ;
975
992
976
993
fn into_iter ( self ) -> Self :: IntoIter {
977
994
self . iter_mut ( )
978
995
}
979
996
}
980
997
981
- impl < K , V , B : BuildHasher , const N : usize > IntoIterator for ArrayMap < K , V , N , B > {
982
- type IntoIter = <ArrayTable < ( K , V ) , N > as IntoIterator >:: IntoIter ;
998
+ impl < K , V , R , B > IntoIterator for ArrayMapFacade < K , V , R , B >
999
+ where
1000
+ R : RawTable < ( K , V ) > ,
1001
+ B : BuildHasher ,
1002
+ {
1003
+ type IntoIter = <R as IntoIterator >:: IntoIter ;
983
1004
type Item = ( K , V ) ;
984
1005
985
1006
fn into_iter ( self ) -> Self :: IntoIter {
986
1007
self . table . into_iter ( )
987
1008
}
988
1009
}
989
1010
990
- impl < K , V , B : BuildHasher + Default , const N : usize > Default for ArrayMap < K , V , N , B > {
1011
+ impl < K , V , R , B > Default for ArrayMapFacade < K , V , R , B >
1012
+ where
1013
+ R : RawTable < ( K , V ) > + Default ,
1014
+ B : BuildHasher + Default ,
1015
+ {
991
1016
fn default ( ) -> Self {
992
1017
Self :: with_hasher ( B :: default ( ) )
993
1018
}
994
1019
}
995
1020
996
- impl < K , V , B : BuildHasher , const N : usize > fmt:: Debug for ArrayMap < K , V , N , B >
1021
+ impl < K , V , R , B > fmt:: Debug for ArrayMapFacade < K , V , R , B >
997
1022
where
998
1023
K : fmt:: Debug ,
999
1024
V : fmt:: Debug ,
1025
+ R : RawTableIter < ( K , V ) > ,
1026
+ B : BuildHasher ,
1000
1027
{
1001
1028
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1002
1029
f. debug_map ( ) . entries ( self . iter ( ) ) . finish ( )
1003
1030
}
1004
1031
}
1005
1032
1006
- impl < K , V , B , const N : usize > PartialEq < Self > for ArrayMap < K , V , N , B >
1033
+ impl < K , V , R , B > PartialEq < Self > for ArrayMapFacade < K , V , R , B >
1007
1034
where
1008
1035
K : Eq + Hash ,
1009
1036
V : PartialEq ,
1037
+ R : RawTableIter < ( K , V ) > ,
1010
1038
B : BuildHasher ,
1011
1039
{
1012
1040
fn eq ( & self , other : & Self ) -> bool {
@@ -1019,23 +1047,25 @@ where
1019
1047
}
1020
1048
}
1021
1049
1022
- impl < K , V , B , const N : usize > Eq for ArrayMap < K , V , N , B >
1050
+ impl < K , V , R , B > Eq for ArrayMapFacade < K , V , R , B >
1023
1051
where
1024
1052
K : Eq + Hash ,
1025
1053
V : PartialEq ,
1054
+ R : RawTableIter < ( K , V ) > ,
1026
1055
B : BuildHasher ,
1027
1056
{
1028
1057
}
1029
1058
1030
- impl < K , V , B , const N : usize > TryFromIterator < ( K , V ) > for ArrayMap < K , V , N , B >
1059
+ impl < K , V , R , B > TryFromIterator < ( K , V ) > for ArrayMapFacade < K , V , R , B >
1031
1060
where
1032
1061
K : Eq + Hash ,
1062
+ R : RawTable < ( K , V ) > + Default ,
1033
1063
B : BuildHasher + Default ,
1034
1064
{
1035
1065
type Error = CapacityError ;
1036
1066
1037
1067
fn try_from_iter < T : IntoIterator < Item = ( K , V ) > > ( iter : T ) -> Result < Self , Self :: Error > {
1038
- let mut result = ArrayMap :: with_build_hasher ( B :: default ( ) ) ;
1068
+ let mut result = Self :: with_build_hasher ( B :: default ( ) ) ;
1039
1069
1040
1070
for ( key, value) in iter {
1041
1071
result. insert ( key, value) ?;
@@ -1045,9 +1075,10 @@ where
1045
1075
}
1046
1076
}
1047
1077
1048
- impl < K , V , B , const N : usize > TryExtend < ( K , V ) > for ArrayMap < K , V , N , B >
1078
+ impl < K , V , R , B > TryExtend < ( K , V ) > for ArrayMapFacade < K , V , R , B >
1049
1079
where
1050
1080
K : Eq + Hash ,
1081
+ R : RawTable < ( K , V ) > ,
1051
1082
B : BuildHasher ,
1052
1083
{
1053
1084
type Error = CapacityError ;
@@ -1061,10 +1092,11 @@ where
1061
1092
}
1062
1093
}
1063
1094
1064
- impl < ' a , K , V , B , const N : usize > TryExtend < ( & ' a K , & ' a V ) > for ArrayMap < K , V , N , B >
1095
+ impl < ' a , K , V , R , B > TryExtend < ( & ' a K , & ' a V ) > for ArrayMapFacade < K , V , R , B >
1065
1096
where
1066
1097
K : Eq + Hash + Copy ,
1067
1098
V : Copy ,
1099
+ R : RawTable < ( K , V ) > ,
1068
1100
B : BuildHasher ,
1069
1101
{
1070
1102
type Error = CapacityError ;
0 commit comments