Skip to content

Commit 3b92689

Browse files
Relax bounds on HashMap to match hashbrown
No functional changes are made, and all APIs are moved to strictly less restrictive bounds. These APIs changed from the old bound listed to no trait bounds: K: Hash + Eq * new * with_capacity K: Eq + Hash, S: BuildHasher * with_hasher * with_capacity_and_hasher * hasher K: Eq + Hash + Debug -> K: Debug S: BuildHasher -> S <HashMap as Debug> K: Eq + Hash -> K S: BuildHasher + Default -> S: Default <HashMap as Default>
1 parent 3ac40b6 commit 3b92689

File tree

1 file changed

+62
-64
lines changed
  • src/libstd/collections/hash

1 file changed

+62
-64
lines changed

src/libstd/collections/hash/map.rs

+62-64
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub struct HashMap<K, V, S = RandomState> {
203203
base: base::HashMap<K, V, S>,
204204
}
205205

206-
impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
206+
impl<K, V> HashMap<K, V, RandomState> {
207207
/// Creates an empty `HashMap`.
208208
///
209209
/// 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> {
240240
}
241241

242242
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+
243296
/// Returns the number of elements the map can hold without reallocating.
244297
///
245298
/// 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> {
457510
pub fn clear(&mut self) {
458511
self.base.clear();
459512
}
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-
}
519513

520514
/// Returns a reference to the map's [`BuildHasher`].
521515
///
@@ -536,7 +530,13 @@ where
536530
pub fn hasher(&self) -> &S {
537531
self.base.hasher()
538532
}
533+
}
539534

535+
impl<K, V, S> HashMap<K, V, S>
536+
where
537+
K: Eq + Hash,
538+
S: BuildHasher,
539+
{
540540
/// Reserves capacity for at least `additional` more elements to be inserted
541541
/// in the `HashMap`. The collection may reserve more space to avoid
542542
/// frequent reallocations.
@@ -984,9 +984,8 @@ where
984984
#[stable(feature = "rust1", since = "1.0.0")]
985985
impl<K, V, S> Debug for HashMap<K, V, S>
986986
where
987-
K: Eq + Hash + Debug,
987+
K: Debug,
988988
V: Debug,
989-
S: BuildHasher,
990989
{
991990
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
992991
f.debug_map().entries(self.iter()).finish()
@@ -996,8 +995,7 @@ where
996995
#[stable(feature = "rust1", since = "1.0.0")]
997996
impl<K, V, S> Default for HashMap<K, V, S>
998997
where
999-
K: Eq + Hash,
1000-
S: BuildHasher + Default,
998+
S: Default,
1001999
{
10021000
/// Creates an empty `HashMap<K, V, S>`, with the `Default` value for the hasher.
10031001
#[inline]

0 commit comments

Comments
 (0)