Skip to content

Commit 101c045

Browse files
committed
Auto merge of #185 - mbrubeck:bounds, r=Amanieu
Relax bounds on HashSet constructors Relax bounds on `HashSet::with_hasher` and `with_capacity_and_hasher` to match the bounds on `HashMap` and on `std::collections::HashSet`. This will allow `std::collections::HashSet` to be based on `hashbrown::HashSet`. See also rust-lang/rust#67642
2 parents 18ea4fe + 8726b29 commit 101c045

File tree

1 file changed

+73
-73
lines changed

1 file changed

+73
-73
lines changed

src/set.rs

+73-73
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<T: Clone, S: Clone> Clone for HashSet<T, S> {
130130
}
131131

132132
#[cfg(feature = "ahash")]
133-
impl<T: Hash + Eq> HashSet<T, DefaultHashBuilder> {
133+
impl<T> HashSet<T, DefaultHashBuilder> {
134134
/// Creates an empty `HashSet`.
135135
///
136136
/// The hash set is initially created with a capacity of 0, so it will not allocate until it
@@ -170,6 +170,72 @@ impl<T: Hash + Eq> HashSet<T, DefaultHashBuilder> {
170170
}
171171

172172
impl<T, S> HashSet<T, S> {
173+
/// Creates a new empty hash set which will use the given hasher to hash
174+
/// keys.
175+
///
176+
/// The hash set is also created with the default initial capacity.
177+
///
178+
/// Warning: `hasher` is normally randomly generated, and
179+
/// is designed to allow `HashSet`s to be resistant to attacks that
180+
/// cause many collisions and very poor performance. Setting it
181+
/// manually using this function can expose a DoS attack vector.
182+
///
183+
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
184+
/// the HashMap to be useful, see its documentation for details.
185+
///
186+
///
187+
/// # Examples
188+
///
189+
/// ```
190+
/// use hashbrown::HashSet;
191+
/// use hashbrown::hash_map::DefaultHashBuilder;
192+
///
193+
/// let s = DefaultHashBuilder::default();
194+
/// let mut set = HashSet::with_hasher(s);
195+
/// set.insert(2);
196+
/// ```
197+
///
198+
/// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
199+
#[cfg_attr(feature = "inline-more", inline)]
200+
pub fn with_hasher(hasher: S) -> Self {
201+
Self {
202+
map: HashMap::with_hasher(hasher),
203+
}
204+
}
205+
206+
/// Creates an empty `HashSet` with the specified capacity, using
207+
/// `hasher` to hash the keys.
208+
///
209+
/// The hash set will be able to hold at least `capacity` elements without
210+
/// reallocating. If `capacity` is 0, the hash set will not allocate.
211+
///
212+
/// Warning: `hasher` is normally randomly generated, and
213+
/// is designed to allow `HashSet`s to be resistant to attacks that
214+
/// cause many collisions and very poor performance. Setting it
215+
/// manually using this function can expose a DoS attack vector.
216+
///
217+
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
218+
/// the HashMap to be useful, see its documentation for details.
219+
///
220+
/// # Examples
221+
///
222+
/// ```
223+
/// use hashbrown::HashSet;
224+
/// use hashbrown::hash_map::DefaultHashBuilder;
225+
///
226+
/// let s = DefaultHashBuilder::default();
227+
/// let mut set = HashSet::with_capacity_and_hasher(10, s);
228+
/// set.insert(1);
229+
/// ```
230+
///
231+
/// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
232+
#[cfg_attr(feature = "inline-more", inline)]
233+
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
234+
Self {
235+
map: HashMap::with_capacity_and_hasher(capacity, hasher),
236+
}
237+
}
238+
173239
/// Returns the number of elements the set can hold without reallocating.
174240
///
175241
/// # Examples
@@ -335,78 +401,6 @@ impl<T, S> HashSet<T, S> {
335401
pub fn clear(&mut self) {
336402
self.map.clear()
337403
}
338-
}
339-
340-
impl<T, S> HashSet<T, S>
341-
where
342-
T: Eq + Hash,
343-
S: BuildHasher,
344-
{
345-
/// Creates a new empty hash set which will use the given hasher to hash
346-
/// keys.
347-
///
348-
/// The hash set is also created with the default initial capacity.
349-
///
350-
/// Warning: `hasher` is normally randomly generated, and
351-
/// is designed to allow `HashSet`s to be resistant to attacks that
352-
/// cause many collisions and very poor performance. Setting it
353-
/// manually using this function can expose a DoS attack vector.
354-
///
355-
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
356-
/// the HashMap to be useful, see its documentation for details.
357-
///
358-
///
359-
/// # Examples
360-
///
361-
/// ```
362-
/// use hashbrown::HashSet;
363-
/// use hashbrown::hash_map::DefaultHashBuilder;
364-
///
365-
/// let s = DefaultHashBuilder::default();
366-
/// let mut set = HashSet::with_hasher(s);
367-
/// set.insert(2);
368-
/// ```
369-
///
370-
/// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
371-
#[cfg_attr(feature = "inline-more", inline)]
372-
pub fn with_hasher(hasher: S) -> Self {
373-
Self {
374-
map: HashMap::with_hasher(hasher),
375-
}
376-
}
377-
378-
/// Creates an empty `HashSet` with the specified capacity, using
379-
/// `hasher` to hash the keys.
380-
///
381-
/// The hash set will be able to hold at least `capacity` elements without
382-
/// reallocating. If `capacity` is 0, the hash set will not allocate.
383-
///
384-
/// Warning: `hasher` is normally randomly generated, and
385-
/// is designed to allow `HashSet`s to be resistant to attacks that
386-
/// cause many collisions and very poor performance. Setting it
387-
/// manually using this function can expose a DoS attack vector.
388-
///
389-
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
390-
/// the HashMap to be useful, see its documentation for details.
391-
///
392-
/// # Examples
393-
///
394-
/// ```
395-
/// use hashbrown::HashSet;
396-
/// use hashbrown::hash_map::DefaultHashBuilder;
397-
///
398-
/// let s = DefaultHashBuilder::default();
399-
/// let mut set = HashSet::with_capacity_and_hasher(10, s);
400-
/// set.insert(1);
401-
/// ```
402-
///
403-
/// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
404-
#[cfg_attr(feature = "inline-more", inline)]
405-
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
406-
Self {
407-
map: HashMap::with_capacity_and_hasher(capacity, hasher),
408-
}
409-
}
410404

411405
/// Returns a reference to the set's [`BuildHasher`].
412406
///
@@ -426,7 +420,13 @@ where
426420
pub fn hasher(&self) -> &S {
427421
self.map.hasher()
428422
}
423+
}
429424

425+
impl<T, S> HashSet<T, S>
426+
where
427+
T: Eq + Hash,
428+
S: BuildHasher,
429+
{
430430
/// Reserves capacity for at least `additional` more elements to be inserted
431431
/// in the `HashSet`. The collection may reserve more space to avoid
432432
/// frequent reallocations.

0 commit comments

Comments
 (0)