1use super::VecMap;
2use alloc::vec::Vec;
3use core::borrow::Borrow;
4use core::ops::{Index, IndexMut};
5
6impl<K, V> Default for VecMap<K, V> {
7 fn default() -> Self {
8 VecMap::new()
9 }
10}
11
12impl<K, V, Q> Index<&Q> for VecMap<K, V>
13where
14 K: Borrow<Q>,
15 Q: Eq + ?Sized,
16{
17 type Output = V;
18
19 fn index(&self, key: &Q) -> &V {
20 self.get(key).expect("VecMap: key not found")
21 }
22}
23
24impl<K, V, Q> IndexMut<&Q> for VecMap<K, V>
25where
26 K: Borrow<Q>,
27 Q: Eq + ?Sized,
28{
29 fn index_mut(&mut self, key: &Q) -> &mut V {
30 self.get_mut(key).expect("VecMap: key not found")
31 }
32}
33
34impl<K, V> Index<usize> for VecMap<K, V> {
35 type Output = V;
36
37 fn index(&self, index: usize) -> &V {
38 self.get_index(index)
39 .expect("VecMap: index out of bounds")
40 .1
41 }
42}
43
44impl<K, V> IndexMut<usize> for VecMap<K, V> {
45 fn index_mut(&mut self, index: usize) -> &mut V {
46 self.get_index_mut(index)
47 .expect("VecMap: index out of bounds")
48 .1
49 }
50}
51
52impl<K, V> Extend<(K, V)> for VecMap<K, V>
53where
54 K: Eq,
55{
56 fn extend<I>(&mut self, iterable: I)
57 where
58 I: IntoIterator<Item = (K, V)>,
59 {
60 self.base
61 .extend(iterable.into_iter().map(|(k, v)| crate::Slot::new(k, v)));
62 }
63}
64
65impl<'a, K: Clone + Eq, V: Clone> Extend<(&'a K, &'a V)> for VecMap<K, V> {
66 fn extend<I>(&mut self, iterable: I)
67 where
68 I: IntoIterator<Item = (&'a K, &'a V)>,
69 {
70 self.extend(
71 iterable
72 .into_iter()
73 .map(|(key, value)| (key.clone(), value.clone())),
74 );
75 }
76}
77
78impl<'a, K: Clone + Eq, V: Clone> Extend<&'a (K, V)> for VecMap<K, V> {
79 fn extend<I>(&mut self, iterable: I)
80 where
81 I: IntoIterator<Item = &'a (K, V)>,
82 {
83 self.extend(
84 iterable
85 .into_iter()
86 .map(|(key, value)| (key.clone(), value.clone())),
87 );
88 }
89}
90
91impl<Item, K, V> FromIterator<Item> for VecMap<K, V>
92where
93 Self: Extend<Item>,
94{
95 fn from_iter<I: IntoIterator<Item = Item>>(iter: I) -> Self {
96 let mut map = VecMap::new();
97 map.extend(iter);
98 map
99 }
100}
101
102impl<K, V> From<Vec<(K, V)>> for VecMap<K, V>
103where
104 K: Eq,
105{
106 fn from(mut vec: Vec<(K, V)>) -> Self {
113 crate::dedup(&mut vec, |rhs, lhs| rhs.0 == lhs.0);
114 unsafe { Self::from_vec_unchecked(vec) }
116 }
117}
118
119impl<K, V> From<&[(K, V)]> for VecMap<K, V>
120where
121 K: Clone + Eq,
122 V: Clone,
123{
124 fn from(slice: &[(K, V)]) -> Self {
125 VecMap::from_iter(slice)
126 }
127}
128
129impl<K, V> From<&mut [(K, V)]> for VecMap<K, V>
130where
131 K: Clone + Eq,
132 V: Clone,
133{
134 fn from(slice: &mut [(K, V)]) -> Self {
135 #[allow(clippy::redundant_slicing)]
137 VecMap::from_iter(&slice[..])
138 }
139}
140
141impl<K, V, const N: usize> From<[(K, V); N]> for VecMap<K, V>
142where
143 K: Eq,
144{
145 fn from(arr: [(K, V); N]) -> Self {
146 VecMap::from_iter(arr)
147 }
148}
149
150impl<K, V> PartialEq for VecMap<K, V>
151where
152 K: Eq,
153 V: PartialEq,
154{
155 fn eq(&self, other: &Self) -> bool {
156 self.base == other.base
157 }
158}
159
160impl<K, V> Eq for VecMap<K, V>
161where
162 K: Eq,
163 V: Eq,
164{
165}
166
167#[cfg(test)]
168mod test {
169 use super::*;
170
171 #[test]
172 fn eq() {
173 assert_ne!(VecMap::from([("a", 1)]), VecMap::from([]));
174 assert_ne!(VecMap::from([("a", 1)]), VecMap::from([("b", 2)]));
175 assert_eq!(VecMap::from([("a", 1)]), VecMap::from([("a", 1)]));
176 assert_ne!(VecMap::from([("a", 1)]), VecMap::from([("a", 1), ("b", 2)]));
177 assert_eq!(
178 VecMap::from([("a", 1), ("b", 2)]),
179 VecMap::from([("a", 1), ("b", 2)])
180 );
181 assert_eq!(
182 VecMap::from([("a", 1), ("b", 2)]),
183 VecMap::from([("b", 2), ("a", 1)])
184 );
185 }
186}