data_structure_traits/collections/
hash_map.rs

1macro_rules! impl_hash_map {
2    ($HashMap:ident) => {
3        use core::borrow::Borrow;
4        use core::hash::{BuildHasher, Hash};
5
6        use super::super::super::*;
7
8        impl<K, V, S> Collection for $HashMap<K, V, S>
9        where
10            K: Eq + Hash,
11            S: BuildHasher,
12        {
13            #[inline(always)]
14            fn len(&self) -> usize {
15                $HashMap::<K, V, S>::len(self)
16            }
17        }
18
19        impl<K, V, S> CollectionMut for $HashMap<K, V, S>
20        where
21            K: Eq + Hash,
22            S: BuildHasher,
23        {
24            #[inline(always)]
25            fn clear(&mut self) {
26                $HashMap::<K, V, S>::drain(self);
27            }
28        }
29
30        impl<K, V, S> Create<(K, V)> for $HashMap<K, V, S>
31        where
32            K: Eq + Hash,
33            S: Default + BuildHasher,
34        {
35            #[inline(always)]
36            fn create() -> Self {
37                $HashMap::<K, V, S>::default()
38            }
39            #[inline(always)]
40            fn create_with_capacity(_: usize) -> Self {
41                $HashMap::<K, V, S>::default()
42            }
43
44            #[inline(always)]
45            fn add_element(mut self, (key, value): (K, V)) -> Self {
46                $HashMap::<K, V, S>::insert(&mut self, key, value);
47                self
48            }
49        }
50
51        impl<'a, K, V, S> Insert<K, V> for $HashMap<K, V, S>
52        where
53            K: Eq + Hash,
54            S: BuildHasher,
55        {
56            type Output = Option<V>;
57
58            #[inline]
59            fn insert(&mut self, k: K, v: V) -> Self::Output {
60                $HashMap::<K, V, S>::insert(self, k, v)
61            }
62        }
63
64        impl<K, V, S> Add<(K, V)> for $HashMap<K, V, S>
65        where
66            K: Eq + Hash,
67            S: BuildHasher,
68        {
69            type Output = Option<V>;
70
71            #[inline(always)]
72            fn add(&mut self, (k, v): (K, V)) -> Self::Output {
73                $HashMap::<K, V, S>::insert(self, k, v)
74            }
75        }
76
77        impl<'a, K, Q: ?Sized, V, S> Remove<&'a Q> for $HashMap<K, V, S>
78        where
79            K: Eq + Hash + Borrow<Q>,
80            Q: Eq + Hash,
81            S: BuildHasher,
82        {
83            type Output = Option<V>;
84
85            #[inline]
86            fn remove(&mut self, q: &Q) -> Self::Output {
87                $HashMap::<K, V, S>::remove(self, q)
88            }
89        }
90
91        impl<'a, K, Q: ?Sized, V, S> Get<&'a Q> for $HashMap<K, V, S>
92        where
93            K: Eq + Hash + Borrow<Q>,
94            Q: Eq + Hash,
95            S: BuildHasher,
96        {
97            type Output = V;
98
99            #[inline(always)]
100            fn get(&self, q: &Q) -> Option<&Self::Output> {
101                $HashMap::<K, V, S>::get(self, q)
102            }
103        }
104        impl<'a, K, Q: ?Sized, V, S> GetMut<&'a Q> for $HashMap<K, V, S>
105        where
106            K: Eq + Hash + Borrow<Q>,
107            Q: Eq + Hash,
108            S: BuildHasher,
109        {
110            #[inline(always)]
111            fn get_mut(&mut self, q: &Q) -> Option<&mut Self::Output> {
112                $HashMap::<K, V, S>::get_mut(self, q)
113            }
114        }
115    };
116}
117
118#[cfg(feature = "hashmap_core")]
119mod __impl_hash_map {
120    use hashmap_core::HashMap;
121    impl_hash_map!(HashMap);
122}
123
124#[cfg(feature = "std")]
125mod __impl_hash_map {
126    use std::collections::hash_map::HashMap;
127    impl_hash_map!(HashMap);
128}