foldhash/
convenience.rs

1use super::fast::{FixedState, RandomState};
2
3/// Type alias for [`std::collections::HashMap<K, V, foldhash::fast::RandomState>`].
4pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
5
6/// Type alias for [`std::collections::HashSet<T, foldhash::fast::RandomState>`].
7pub type HashSet<T> = std::collections::HashSet<T, RandomState>;
8
9/// A convenience extension trait to enable [`HashMap::new`] for hash maps that use `foldhash`.
10pub trait HashMapExt {
11    /// Creates an empty `HashMap`.
12    fn new() -> Self;
13
14    /// Creates an empty `HashMap` with at least the specified capacity.
15    fn with_capacity(capacity: usize) -> Self;
16}
17
18impl<K, V> HashMapExt for std::collections::HashMap<K, V, RandomState> {
19    fn new() -> Self {
20        Self::with_hasher(RandomState::default())
21    }
22
23    fn with_capacity(capacity: usize) -> Self {
24        Self::with_capacity_and_hasher(capacity, RandomState::default())
25    }
26}
27
28impl<K, V> HashMapExt for std::collections::HashMap<K, V, FixedState> {
29    fn new() -> Self {
30        Self::with_hasher(FixedState::default())
31    }
32
33    fn with_capacity(capacity: usize) -> Self {
34        Self::with_capacity_and_hasher(capacity, FixedState::default())
35    }
36}
37
38/// A convenience extension trait to enable [`HashSet::new`] for hash sets that use `foldhash`.
39pub trait HashSetExt {
40    /// Creates an empty `HashSet`.
41    fn new() -> Self;
42
43    /// Creates an empty `HashSet` with at least the specified capacity.
44    fn with_capacity(capacity: usize) -> Self;
45}
46
47impl<T> HashSetExt for std::collections::HashSet<T, RandomState> {
48    fn new() -> Self {
49        Self::with_hasher(RandomState::default())
50    }
51
52    fn with_capacity(capacity: usize) -> Self {
53        Self::with_capacity_and_hasher(capacity, RandomState::default())
54    }
55}
56
57impl<T> HashSetExt for std::collections::HashSet<T, FixedState> {
58    fn new() -> Self {
59        Self::with_hasher(FixedState::default())
60    }
61
62    fn with_capacity(capacity: usize) -> Self {
63        Self::with_capacity_and_hasher(capacity, FixedState::default())
64    }
65}