foldhash/
convenience.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use super::fast::{FixedState, RandomState};

/// Type alias for [`std::collections::HashMap<K, V, foldhash::fast::RandomState>`].
pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;

/// Type alias for [`std::collections::HashSet<T, foldhash::fast::RandomState>`].
pub type HashSet<T> = std::collections::HashSet<T, RandomState>;

/// A convenience extension trait to enable [`HashMap::new`] for hash maps that use `foldhash`.
pub trait HashMapExt {
    /// Creates an empty `HashMap`.
    fn new() -> Self;

    /// Creates an empty `HashMap` with at least the specified capacity.
    fn with_capacity(capacity: usize) -> Self;
}

impl<K, V> HashMapExt for std::collections::HashMap<K, V, RandomState> {
    fn new() -> Self {
        Self::with_hasher(RandomState::default())
    }

    fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity_and_hasher(capacity, RandomState::default())
    }
}

impl<K, V> HashMapExt for std::collections::HashMap<K, V, FixedState> {
    fn new() -> Self {
        Self::with_hasher(FixedState::default())
    }

    fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity_and_hasher(capacity, FixedState::default())
    }
}

/// A convenience extension trait to enable [`HashSet::new`] for hash sets that use `foldhash`.
pub trait HashSetExt {
    /// Creates an empty `HashSet`.
    fn new() -> Self;

    /// Creates an empty `HashSet` with at least the specified capacity.
    fn with_capacity(capacity: usize) -> Self;
}

impl<T> HashSetExt for std::collections::HashSet<T, RandomState> {
    fn new() -> Self {
        Self::with_hasher(RandomState::default())
    }

    fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity_and_hasher(capacity, RandomState::default())
    }
}

impl<T> HashSetExt for std::collections::HashSet<T, FixedState> {
    fn new() -> Self {
        Self::with_hasher(FixedState::default())
    }

    fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity_and_hasher(capacity, FixedState::default())
    }
}