#[cfg(test)]
mod tests;
use {
crate::{
linear_storage::LinearStorage,
map::StableMap,
pos_vec::pos::{InUse, Pos},
},
core::{
fmt::{Debug, Formatter},
iter::FusedIterator,
},
hashbrown::hash_map,
};
pub struct Iter<'a, K, V> {
pub(crate) iter: hash_map::Iter<'a, K, Pos<InUse>>,
pub(crate) entries: &'a LinearStorage<V>,
}
impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
let (k, pos) = self.iter.next()?;
let v = unsafe { self.entries.get_unchecked(pos) };
Some((k, v))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V, S> IntoIterator for &'a StableMap<K, V, S> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<K, V> Clone for Iter<'_, K, V> {
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
entries: self.entries,
}
}
}
impl<K, V> Debug for Iter<'_, K, V>
where
K: Debug,
V: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}
impl<K, V> FusedIterator for Iter<'_, K, V> {}
impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
fn len(&self) -> usize {
self.iter.len()
}
}
unsafe impl<K, V> Send for Iter<'_, K, V>
where
K: Send,
V: Send,
{
}
unsafe impl<K, V> Sync for Iter<'_, K, V>
where
K: Sync,
V: Sync,
{
}