1use core::fmt;
5use core::ptr;
6
7use kevy_hash::KevyHash;
8
9use crate::map::{EMPTY, KevyMap};
10
11pub struct IntoIter<K, V> {
21 map: KevyMap<K, V>,
22 pos: usize,
23}
24
25impl<K, V> IntoIter<K, V> {
26 pub(crate) fn new(map: KevyMap<K, V>) -> Self {
27 IntoIter { map, pos: 0 }
28 }
29}
30
31impl<K, V> Iterator for IntoIter<K, V> {
32 type Item = (K, V);
33
34 fn next(&mut self) -> Option<(K, V)> {
35 while self.pos < self.map.cap {
36 let i = self.pos;
37 self.pos += 1;
38 let meta = unsafe { self.map.metadata_ptr.as_ptr().add(i) };
40 if unsafe { *meta } & 0x80 == 0 {
42 unsafe { *meta = EMPTY };
46 self.map.occupied -= 1;
47 return Some(unsafe { ptr::read(self.map.slots_ptr.as_ptr().add(i).cast()) });
49 }
50 }
51 None
52 }
53
54 fn size_hint(&self) -> (usize, Option<usize>) {
55 (self.map.occupied, Some(self.map.occupied))
56 }
57}
58
59impl<K, V> ExactSizeIterator for IntoIter<K, V> {}
60
61impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 f.debug_struct("IntoIter").field("remaining", &self.map.occupied).finish_non_exhaustive()
64 }
65}
66
67impl<K, V> IntoIterator for KevyMap<K, V> {
68 type Item = (K, V);
69 type IntoIter = IntoIter<K, V>;
70
71 fn into_iter(self) -> IntoIter<K, V> {
72 IntoIter::new(self)
73 }
74}
75
76impl<K, V> PartialEq for KevyMap<K, V>
90where
91 K: KevyHash + Eq,
92 V: PartialEq,
93{
94 fn eq(&self, other: &Self) -> bool {
95 self.len() == other.len() && self.iter().all(|(k, v)| other.get(k) == Some(v))
96 }
97}
98
99impl<K: KevyHash + Eq, V: Eq> Eq for KevyMap<K, V> {}