@@ -203,7 +203,7 @@ pub struct HashMap<K, V, S = RandomState> {
203
203
base : base:: HashMap < K , V , S > ,
204
204
}
205
205
206
- impl < K : Hash + Eq , V > HashMap < K , V , RandomState > {
206
+ impl < K , V > HashMap < K , V , RandomState > {
207
207
/// Creates an empty `HashMap`.
208
208
///
209
209
/// The hash map is initially created with a capacity of 0, so it will not allocate until it
@@ -240,6 +240,59 @@ impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
240
240
}
241
241
242
242
impl < K , V , S > HashMap < K , V , S > {
243
+ /// Creates an empty `HashMap` which will use the given hash builder to hash
244
+ /// keys.
245
+ ///
246
+ /// The created map has the default initial capacity.
247
+ ///
248
+ /// Warning: `hash_builder` is normally randomly generated, and
249
+ /// is designed to allow HashMaps to be resistant to attacks that
250
+ /// cause many collisions and very poor performance. Setting it
251
+ /// manually using this function can expose a DoS attack vector.
252
+ ///
253
+ /// # Examples
254
+ ///
255
+ /// ```
256
+ /// use std::collections::HashMap;
257
+ /// use std::collections::hash_map::RandomState;
258
+ ///
259
+ /// let s = RandomState::new();
260
+ /// let mut map = HashMap::with_hasher(s);
261
+ /// map.insert(1, 2);
262
+ /// ```
263
+ #[ inline]
264
+ #[ stable( feature = "hashmap_build_hasher" , since = "1.7.0" ) ]
265
+ pub fn with_hasher ( hash_builder : S ) -> HashMap < K , V , S > {
266
+ HashMap { base : base:: HashMap :: with_hasher ( hash_builder) }
267
+ }
268
+
269
+ /// Creates an empty `HashMap` with the specified capacity, using `hash_builder`
270
+ /// to hash the keys.
271
+ ///
272
+ /// The hash map will be able to hold at least `capacity` elements without
273
+ /// reallocating. If `capacity` is 0, the hash map will not allocate.
274
+ ///
275
+ /// Warning: `hash_builder` is normally randomly generated, and
276
+ /// is designed to allow HashMaps to be resistant to attacks that
277
+ /// cause many collisions and very poor performance. Setting it
278
+ /// manually using this function can expose a DoS attack vector.
279
+ ///
280
+ /// # Examples
281
+ ///
282
+ /// ```
283
+ /// use std::collections::HashMap;
284
+ /// use std::collections::hash_map::RandomState;
285
+ ///
286
+ /// let s = RandomState::new();
287
+ /// let mut map = HashMap::with_capacity_and_hasher(10, s);
288
+ /// map.insert(1, 2);
289
+ /// ```
290
+ #[ inline]
291
+ #[ stable( feature = "hashmap_build_hasher" , since = "1.7.0" ) ]
292
+ pub fn with_capacity_and_hasher ( capacity : usize , hash_builder : S ) -> HashMap < K , V , S > {
293
+ HashMap { base : base:: HashMap :: with_capacity_and_hasher ( capacity, hash_builder) }
294
+ }
295
+
243
296
/// Returns the number of elements the map can hold without reallocating.
244
297
///
245
298
/// This number is a lower bound; the `HashMap<K, V>` might be able to hold
@@ -457,65 +510,6 @@ impl<K, V, S> HashMap<K, V, S> {
457
510
pub fn clear ( & mut self ) {
458
511
self . base . clear ( ) ;
459
512
}
460
- }
461
-
462
- impl < K , V , S > HashMap < K , V , S >
463
- where
464
- K : Eq + Hash ,
465
- S : BuildHasher ,
466
- {
467
- /// Creates an empty `HashMap` which will use the given hash builder to hash
468
- /// keys.
469
- ///
470
- /// The created map has the default initial capacity.
471
- ///
472
- /// Warning: `hash_builder` is normally randomly generated, and
473
- /// is designed to allow HashMaps to be resistant to attacks that
474
- /// cause many collisions and very poor performance. Setting it
475
- /// manually using this function can expose a DoS attack vector.
476
- ///
477
- /// # Examples
478
- ///
479
- /// ```
480
- /// use std::collections::HashMap;
481
- /// use std::collections::hash_map::RandomState;
482
- ///
483
- /// let s = RandomState::new();
484
- /// let mut map = HashMap::with_hasher(s);
485
- /// map.insert(1, 2);
486
- /// ```
487
- #[ inline]
488
- #[ stable( feature = "hashmap_build_hasher" , since = "1.7.0" ) ]
489
- pub fn with_hasher ( hash_builder : S ) -> HashMap < K , V , S > {
490
- HashMap { base : base:: HashMap :: with_hasher ( hash_builder) }
491
- }
492
-
493
- /// Creates an empty `HashMap` with the specified capacity, using `hash_builder`
494
- /// to hash the keys.
495
- ///
496
- /// The hash map will be able to hold at least `capacity` elements without
497
- /// reallocating. If `capacity` is 0, the hash map will not allocate.
498
- ///
499
- /// Warning: `hash_builder` is normally randomly generated, and
500
- /// is designed to allow HashMaps to be resistant to attacks that
501
- /// cause many collisions and very poor performance. Setting it
502
- /// manually using this function can expose a DoS attack vector.
503
- ///
504
- /// # Examples
505
- ///
506
- /// ```
507
- /// use std::collections::HashMap;
508
- /// use std::collections::hash_map::RandomState;
509
- ///
510
- /// let s = RandomState::new();
511
- /// let mut map = HashMap::with_capacity_and_hasher(10, s);
512
- /// map.insert(1, 2);
513
- /// ```
514
- #[ inline]
515
- #[ stable( feature = "hashmap_build_hasher" , since = "1.7.0" ) ]
516
- pub fn with_capacity_and_hasher ( capacity : usize , hash_builder : S ) -> HashMap < K , V , S > {
517
- HashMap { base : base:: HashMap :: with_capacity_and_hasher ( capacity, hash_builder) }
518
- }
519
513
520
514
/// Returns a reference to the map's [`BuildHasher`].
521
515
///
@@ -536,7 +530,13 @@ where
536
530
pub fn hasher ( & self ) -> & S {
537
531
self . base . hasher ( )
538
532
}
533
+ }
539
534
535
+ impl < K , V , S > HashMap < K , V , S >
536
+ where
537
+ K : Eq + Hash ,
538
+ S : BuildHasher ,
539
+ {
540
540
/// Reserves capacity for at least `additional` more elements to be inserted
541
541
/// in the `HashMap`. The collection may reserve more space to avoid
542
542
/// frequent reallocations.
@@ -984,9 +984,8 @@ where
984
984
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
985
985
impl < K , V , S > Debug for HashMap < K , V , S >
986
986
where
987
- K : Eq + Hash + Debug ,
987
+ K : Debug ,
988
988
V : Debug ,
989
- S : BuildHasher ,
990
989
{
991
990
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
992
991
f. debug_map ( ) . entries ( self . iter ( ) ) . finish ( )
@@ -996,8 +995,7 @@ where
996
995
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
997
996
impl < K , V , S > Default for HashMap < K , V , S >
998
997
where
999
- K : Eq + Hash ,
1000
- S : BuildHasher + Default ,
998
+ S : Default ,
1001
999
{
1002
1000
/// Creates an empty `HashMap<K, V, S>`, with the `Default` value for the hasher.
1003
1001
#[ inline]
0 commit comments