1use crate::mem::{NonNull, PhantomData};
2
3use super::Key;
4
5
6#[repr(transparent)]
7pub struct KSlice<K: Key, V> {
8 phantom: PhantomData<fn (K) -> K>,
9 inner: [V],
10}
11
12impl<K: Key, V> KSlice<K, V> {
13 #[inline]
14 pub const fn new_unck<'a>(slice: &'a [V]) -> &'a Self {
15 unsafe { &*(slice as *const [V] as *const Self) }
16 }
17
18 #[inline]
19 pub fn new_mut_unck<'a>(slice: &'a mut [V]) -> &'a mut Self {
20 unsafe { &mut *(slice as *mut [V] as *mut Self) }
21 }
22
23 #[inline]
24 pub const fn empty<'a>() -> &'a Self { Self::new_unck(&[]) }
25
26 #[inline]
27 pub fn empty_mut<'a>() -> &'a mut Self { Self::new_mut_unck(&mut []) }
28
29
30 #[inline(always)]
31 pub const fn inner(&self) -> &[V] { &self.inner }
32
33 #[inline(always)]
34 pub fn inner_mut(&mut self) -> &mut [V] { &mut self.inner }
35
36 #[inline(always)]
37 pub const fn len(&self) -> usize { self.inner.len() }
38
39
40 #[inline]
41 pub fn iter(&self) -> KIter<K, V> {
42 KIter {
43 ptr: unsafe { NonNull::new_unchecked(self.inner.as_ptr() as *mut V) },
44 len: self.inner.len(),
45 idx: 0,
46 phantom: PhantomData,
47 }
48 }
49
50 #[inline]
51 pub fn iter_mut(&mut self) -> KIterMut<K, V> {
52 KIterMut {
53 ptr: unsafe { NonNull::new_unchecked(self.inner.as_mut_ptr() as *mut V) },
54 len: self.inner.len(),
55 idx: 0,
56 phantom: PhantomData,
57 }
58 }
59
60
61 #[inline(always)]
62 pub fn get(&self, index: K) -> Option<&V> {
63 self.inner.get(index.usize())
64 }
65
66 #[inline(always)]
67 pub fn get_mut(&mut self, index: K) -> Option<&mut V> {
68 self.inner.get_mut(index.usize())
69 }
70}
71
72
73impl<'a, K: Key, V> Default for &'a KSlice<K, V> {
74 #[inline]
75 fn default() -> Self {
76 KSlice::new_unck(&[])
77 }
78}
79
80
81impl<K: Key, V> core::fmt::Debug for KSlice<K, V>
82where K: core::fmt::Debug, V: core::fmt::Debug {
83 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
84 f.debug_map().entries(self.iter()).finish()
85 }
86}
87
88
89impl<K: Key, V> core::ops::Index<K> for KSlice<K, V> {
90 type Output = V;
91
92 #[inline]
93 fn index(&self, index: K) -> &Self::Output {
94 &self.inner[index.usize()]
95 }
96}
97
98impl<K: Key, V> core::ops::IndexMut<K> for KSlice<K, V> {
99 #[inline]
100 fn index_mut(&mut self, index: K) -> &mut Self::Output {
101 &mut self.inner[index.usize()]
102 }
103}
104
105
106impl<K: Key, V: PartialEq> PartialEq for KSlice<K, V> {
107 #[inline]
108 fn eq(&self, other: &Self) -> bool {
109 self.inner() == other.inner()
110 }
111}
112
113impl<K: Key, V: Eq> Eq for KSlice<K, V> {}
114
115
116
117#[must_use = "iterators are lazy and do nothing unless consumed"]
118#[derive(Clone)]
119pub struct KIter<'a, K: Key, V> {
120 ptr: NonNull<V>,
121 len: usize,
122 idx: usize, phantom: PhantomData<(K, &'a V)>,
124}
125
126impl<'a, K: Key, V> Iterator for KIter<'a, K, V> {
127 type Item = (K, &'a V);
128
129 #[inline]
130 fn next(&mut self) -> Option<Self::Item> {
131 if self.idx < self.len {
132 let k = K::from_usize_unck(self.idx);
133 let v = unsafe { &*self.ptr.as_ptr().add(self.idx) };
134 self.idx += 1;
135 return Some((k, v));
136 }
137 None
138 }
139
140 #[inline]
141 fn nth(&mut self, i: usize) -> Option<Self::Item> {
142 if i < self.len - self.idx {
143 self.idx += i;
144
145 let k = K::from_usize_unck(self.idx);
146 let v = unsafe { &*self.ptr.as_ptr().add(self.idx) };
147 self.idx += 1;
148 return Some((k, v));
149 }
150 None
151 }
152
153 #[inline]
154 fn last(self) -> Option<Self::Item> {
155 if self.idx < self.len {
156 let idx = self.len - 1;
157 let k = K::from_usize_unck(idx);
158 let v = unsafe { &*self.ptr.as_ptr().add(idx) };
159 return Some((k, v));
160 }
161 None
162 }
163
164 #[inline]
165 fn size_hint(&self) -> (usize, Option<usize>) {
166 let len = self.len - self.idx;
167 (len, Some(len))
168 }
169}
170
171
172#[must_use = "iterators are lazy and do nothing unless consumed"]
173pub struct KIterMut<'a, K: Key, V> {
174 ptr: NonNull<V>,
175 len: usize,
176 idx: usize, phantom: PhantomData<(K, &'a mut V)>,
178}
179
180impl<'a, K: Key, V> Iterator for KIterMut<'a, K, V> {
181 type Item = (K, &'a mut V);
182
183 #[inline]
184 fn next(&mut self) -> Option<Self::Item> {
185 if self.idx < self.len {
186 let k = K::from_usize_unck(self.idx);
187 let v = unsafe { &mut *self.ptr.as_ptr().add(self.idx) };
188 self.idx += 1;
189 return Some((k, v));
190 }
191 None
192 }
193
194 #[inline]
195 fn nth(&mut self, i: usize) -> Option<Self::Item> {
196 if i < self.len - self.idx {
197 self.idx += i;
198
199 let k = K::from_usize_unck(self.idx);
200 let v = unsafe { &mut *self.ptr.as_ptr().add(self.idx) };
201 self.idx += 1;
202 return Some((k, v));
203 }
204 None
205 }
206
207 #[inline]
208 fn last(self) -> Option<Self::Item> {
209 if self.idx < self.len {
210 let idx = self.len - 1;
211 let k = K::from_usize_unck(idx);
212 let v = unsafe { &mut *self.ptr.as_ptr().add(idx) };
213 return Some((k, v));
214 }
215 None
216 }
217
218 #[inline]
219 fn size_hint(&self) -> (usize, Option<usize>) {
220 let len = self.len - self.idx;
221 (len, Some(len))
222 }
223}
224
225