#[cfg(test)]
mod tests;
use {
crate::{
linear_storage::LinearStorage,
pos_vec::pos::{InUse, Pos},
},
core::{
fmt::{Debug, Formatter},
iter::FusedIterator,
},
hashbrown::hash_map,
};
pub struct Drain<'a, K, V> {
pub(crate) drain: hash_map::Drain<'a, K, Pos<InUse>>,
pub(crate) entries: &'a mut LinearStorage<V>,
}
impl<K, V> Drop for Drain<'_, K, V> {
fn drop(&mut self) {
self.entries.clear();
}
}
impl<K, V> Iterator for Drain<'_, K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
let (k, pos) = self.drain.next()?;
let value = unsafe {
self.entries.take_unchecked(pos)
};
Some((k, value))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.drain.size_hint()
}
}
impl<K, V> Debug for Drain<'_, K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Drain").finish_non_exhaustive()
}
}
impl<K, V> ExactSizeIterator for Drain<'_, K, V> {
fn len(&self) -> usize {
self.drain.len()
}
}
impl<K, V> FusedIterator for Drain<'_, K, V> {}
unsafe impl<K, V> Send for Drain<'_, K, V>
where
K: Send,
V: Send,
{
}
unsafe impl<K, V> Sync for Drain<'_, K, V>
where
K: Sync,
V: Sync,
{
}