1mod handle;
2mod map;
3mod secondary;
4mod slot;
5mod utils;
6
7use std::fmt::Debug;
8use std::hash::Hash;
9
10use std::fmt;
11use std::num::NonZeroU32;
12use std::ops::Deref;
13
14use crate::utils::unlikely;
15
16#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub struct Generation(NonZeroU32);
22
23impl fmt::Display for Generation {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 fmt::Display::fmt(&self.0, f)
26 }
27}
28
29impl Generation {
30 pub const MIN: Self = unsafe { Self(NonZeroU32::new_unchecked(1)) };
31
32 #[inline(always)]
33 #[cfg(feature = "serde")]
34 pub const fn new(val: NonZeroU32) -> Self {
35 Self(val)
36 }
37
38 #[inline(always)]
39 pub const unsafe fn new_unchecked(val: u32) -> Self {
40 unsafe { Self(NonZeroU32::new_unchecked(val)) }
41 }
42}
43
44impl Deref for Generation {
45 type Target = NonZeroU32;
46
47 #[inline(always)]
48 fn deref(&self) -> &Self::Target {
49 &self.0
50 }
51}
52
53#[derive(Clone, Copy, PartialEq, Eq, Debug)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72pub struct Version(NonZeroU32);
73
74impl Version {
75 #[inline(always)]
79 pub fn new(generation: Generation, state: u32) -> Self {
80 debug_assert!(state <= 0b11);
81 let g = generation.0.get();
82 let v = (g << 2) | (state & 0b11);
86 unsafe { Self(NonZeroU32::new_unchecked(v)) }
87 }
88
89 #[inline(always)]
93 pub fn sentinel() -> Self {
94 Self::new(Generation::MIN, 0b00)
95 }
96
97 #[inline(always)]
101 pub fn generation(&self) -> Generation {
102 unsafe { Generation::new_unchecked(self.0.get() >> 2) }
104 }
105
106 #[inline(always)]
110 pub fn state(&self) -> u32 {
111 self.0.get() & 0b11
112 }
113
114 #[inline(always)]
118 pub fn is_vacant(&self) -> bool {
119 self.state() == 0b00
120 }
121
122 #[inline(always)]
126 pub fn is_reserved(&self) -> bool {
127 self.state() == 0b01
128 }
129
130 #[inline(always)]
134 pub fn is_occupied(&self) -> bool {
135 self.state() == 0b11
136 }
137
138 #[inline(always)]
146 pub fn vacant_to_reserved(&mut self) {
147 debug_assert!(self.is_vacant());
148 unsafe {
150 self.0 = NonZeroU32::new_unchecked(self.0.get() + 1);
151 }
152 }
153
154 #[inline(always)]
162 pub fn reserved_to_occupied(&mut self) {
163 debug_assert!(self.is_reserved());
164 unsafe {
166 self.0 = NonZeroU32::new_unchecked(self.0.get() + 2);
167 }
168 }
169
170 #[inline(always)]
178 pub fn occupied_to_vacant(&mut self) {
179 debug_assert!(self.is_occupied());
180 let mut v = self.0.get();
181 v = v.wrapping_add(1);
183
184 if unlikely(v >> 2 == 0) {
186 v = v.wrapping_add(1 << 2);
187 }
188
189 unsafe {
191 self.0 = NonZeroU32::new_unchecked(v);
192 }
193 }
194
195 #[inline(always)]
205 pub fn reserved_to_vacant(&mut self) {
206 debug_assert!(self.is_reserved());
207 let mut v = self.0.get();
208 v = v.wrapping_add(3);
210
211 if unlikely(v >> 2 == 0) {
213 v = v.wrapping_add(1 << 2);
214 }
215
216 unsafe {
217 self.0 = NonZeroU32::new_unchecked(v);
218 }
219 }
220}
221
222pub trait Key: Copy + Clone + PartialEq + Eq + Hash + Debug {
223 type Raw: Copy + Clone + PartialEq + Eq + Hash + Debug;
224 unsafe fn from_raw(raw: Self::Raw, #[cfg(debug_assertions)] map_id: u64) -> Self;
225 fn from_parts(index: u32, generation: Generation, #[cfg(debug_assertions)] map_id: u64)
226 -> Self;
227 fn index(&self) -> u32;
228 fn generation(&self) -> Generation;
229 fn raw(&self) -> Self::Raw;
230 #[cfg(debug_assertions)]
231 fn map_id(&self) -> u64;
232}
233
234#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
235pub struct DefaultKey {
236 pub(crate) raw: u64,
237 #[cfg(debug_assertions)]
238 pub(crate) map_id: u64,
239}
240
241impl Key for DefaultKey {
242 type Raw = u64;
243
244 #[inline(always)]
245 unsafe fn from_raw(raw: Self::Raw, #[cfg(debug_assertions)] map_id: u64) -> Self {
246 Self {
247 raw,
248 #[cfg(debug_assertions)]
249 map_id,
250 }
251 }
252
253 #[inline(always)]
254 fn from_parts(
255 index: u32,
256 generation: Generation,
257 #[cfg(debug_assertions)] map_id: u64,
258 ) -> Self {
259 Self {
260 raw: ((generation.get() as u64) << 32) | (index as u64),
261 #[cfg(debug_assertions)]
262 map_id,
263 }
264 }
265
266 #[inline(always)]
267 fn index(&self) -> u32 {
268 self.raw as u32
269 }
270
271 #[inline(always)]
272 fn generation(&self) -> Generation {
273 unsafe { Generation(NonZeroU32::new_unchecked((self.raw >> 32) as u32)) }
275 }
276
277 #[inline(always)]
278 fn raw(&self) -> Self::Raw {
279 self.raw
280 }
281
282 #[cfg(debug_assertions)]
283 #[inline(always)]
284 fn map_id(&self) -> u64 {
285 self.map_id
286 }
287}
288
289impl DefaultKey {
290 #[inline(always)]
294 pub fn new(index: u32, generation: Generation, #[cfg(debug_assertions)] map_id: u64) -> Self {
295 <Self as Key>::from_parts(
296 index,
297 generation,
298 #[cfg(debug_assertions)]
299 map_id,
300 )
301 }
302
303 #[inline(always)]
307 pub fn decode(&self) -> (u32, Generation) {
308 (self.index(), self.generation())
309 }
310}
311
312pub use handle::Handle;
313pub use map::DeferredMap;
314pub use secondary::SecondaryMap;
315
316#[cfg(test)]
317mod tests {
318 mod debug_safety;
321 mod edge_cases;
322 mod handle;
323 mod insertion;
324 mod new_features;
325 mod removal;
326 mod secondary_test;
327}