1
- use std:: collections:: { HashMap , HashSet } ;
2
1
use std:: hash:: { BuildHasher , Hasher } ;
3
2
4
3
use crate :: gxhash:: platform:: * ;
@@ -159,10 +158,55 @@ impl BuildHasher for GxBuildHasher {
159
158
}
160
159
161
160
/// A `HashMap` using a (DOS-resistant) [`GxBuildHasher`].
162
- pub type GxHashMap < K , V > = HashMap < K , V , GxBuildHasher > ;
161
+ pub type HashMap < K , V > = std:: collections:: HashMap < K , V , GxBuildHasher > ;
162
+
163
+ /// A convenience trait that can be used together with the type aliases defined
164
+ /// to get access to the `new()` and `with_capacity()` methods for the
165
+ /// [`HashMap`] type alias.
166
+ pub trait HashMapExt {
167
+ /// Constructs a new HashMap.
168
+ fn new ( ) -> Self ;
169
+ /// Constructs a new HashMap with a given initial capacity.
170
+ fn with_capacity ( capacity : usize ) -> Self ;
171
+ }
172
+
173
+ impl < K , V , S > HashMapExt for std:: collections:: HashMap < K , V , S >
174
+ where
175
+ S : BuildHasher + Default ,
176
+ {
177
+ fn new ( ) -> Self {
178
+ std:: collections:: HashMap :: with_hasher ( S :: default ( ) )
179
+ }
180
+
181
+ fn with_capacity ( capacity : usize ) -> Self {
182
+ std:: collections:: HashMap :: with_capacity_and_hasher ( capacity, S :: default ( ) )
183
+ }
184
+ }
163
185
164
186
/// A `HashSet` using a (DOS-resistant) [`GxBuildHasher`].
165
- pub type GxHashSet < T > = HashSet < T , GxBuildHasher > ;
187
+ pub type HashSet < T > = std:: collections:: HashSet < T , GxBuildHasher > ;
188
+
189
+ /// A convenience trait that can be used together with the type aliases defined
190
+ /// to get access to the `new()` and `with_capacity()` methods for the
191
+ /// [`HashSet`] type alias.
192
+ pub trait HashSetExt {
193
+ /// Constructs a new HashMap.
194
+ fn new ( ) -> Self ;
195
+ /// Constructs a new HashMap with a given initial capacity.
196
+ fn with_capacity ( capacity : usize ) -> Self ;
197
+ }
198
+
199
+ impl < K , S > HashSetExt for std:: collections:: HashSet < K , S >
200
+ where S : BuildHasher + Default ,
201
+ {
202
+ fn new ( ) -> Self {
203
+ std:: collections:: HashSet :: with_hasher ( S :: default ( ) )
204
+ }
205
+
206
+ fn with_capacity ( capacity : usize ) -> Self {
207
+ std:: collections:: HashSet :: with_capacity_and_hasher ( capacity, S :: default ( ) )
208
+ }
209
+ }
166
210
167
211
#[ cfg( test) ]
168
212
mod tests {
@@ -171,14 +215,31 @@ mod tests {
171
215
172
216
use super :: * ;
173
217
218
+ #[ test]
219
+ fn contructors_work ( ) {
220
+ let mut map: std:: collections:: HashMap < & str , i32 , GxBuildHasher > = HashMap :: new ( ) ;
221
+ assert_eq ! ( true , map. insert( "foo" , 1 ) . is_none( ) ) ;
222
+
223
+ let mut map = HashMap :: with_capacity ( 3 ) ;
224
+ assert_eq ! ( 3 , map. capacity( ) ) ;
225
+ assert_eq ! ( true , map. insert( "bar" , 2 ) . is_none( ) ) ;
226
+
227
+ let mut set: std:: collections:: HashSet < i32 , GxBuildHasher > = HashSet :: new ( ) ;
228
+ assert_eq ! ( true , set. insert( 42 ) ) ;
229
+
230
+ let mut set = HashSet :: with_capacity ( 3 ) ;
231
+ assert_eq ! ( true , set. insert( 42 ) ) ;
232
+ assert_eq ! ( 3 , set. capacity( ) ) ;
233
+ }
234
+
174
235
#[ test]
175
236
fn hasher_produces_stable_hashes ( ) {
176
- let mut hashset = GxHashSet :: default ( ) ;
237
+ let mut hashset = HashSet :: default ( ) ;
177
238
assert ! ( hashset. insert( 1234 ) ) ;
178
239
assert ! ( !hashset. insert( 1234 ) ) ;
179
240
assert ! ( hashset. insert( 42 ) ) ;
180
241
181
- let mut hashset = GxHashSet :: default ( ) ;
242
+ let mut hashset = HashSet :: default ( ) ;
182
243
assert ! ( hashset. insert( "hello" ) ) ;
183
244
assert ! ( hashset. insert( "world" ) ) ;
184
245
assert ! ( !hashset. insert( "hello" ) ) ;
@@ -199,8 +260,8 @@ mod tests {
199
260
// This is important for DOS resistance
200
261
#[ test]
201
262
fn gxhashset_uses_default_gxhasherbuilder ( ) {
202
- let hashset_1 = GxHashSet :: < u32 > :: default ( ) ;
203
- let hashset_2 = GxHashSet :: < u32 > :: default ( ) ;
263
+ let hashset_1 = HashSet :: < u32 > :: default ( ) ;
264
+ let hashset_2 = HashSet :: < u32 > :: default ( ) ;
204
265
205
266
let mut hasher_1 = hashset_1. hasher ( ) . build_hasher ( ) ;
206
267
let mut hasher_2 = hashset_2. hasher ( ) . build_hasher ( ) ;
0 commit comments