Debian Codegraph @

debian/rust-vendor/foldhash/src/convenience.rs

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