use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
use dashmap::iter::Iter as DashIter;
use dashmap::iter::OwningIter as DashOwningIter;
use dashmap::mapref::multiple::RefMulti as DashMulti;
use waitcell::WaitCell;
pub struct Iter<'a, K, V, S = RandomState> {
pub(crate) iter: DashIter<'a, K, Box<WaitCell<V>>, S>,
pub(crate) current: Option<DashMulti<'a, K, Box<WaitCell<V>>, S>>,
}
impl<'a, K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for Iter<'a, K, V, S> {
type Item = (&'a K, &'a WaitCell<V>);
fn next(&mut self) -> Option<Self::Item> {
self.current.take();
self.current = self.iter.next();
self.current.as_ref().map(|multi| unsafe {
(&*(multi.key() as *const K), &*(multi.value().as_ref() as *const WaitCell<V>))
})
}
}
pub struct Keys<'a, K, V, S = RandomState> {
pub(crate) iter: Iter<'a, K, V, S>,
}
impl<'a, K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for Keys<'a, K, V, S> {
type Item = &'a K;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(key, _)| key)
}
}
pub struct Values<'a, K, V, S = RandomState> {
pub(crate) iter: DashIter<'a, K, Box<WaitCell<V>>, S>,
}
impl<'a, K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for Values<'a, K, V, S> {
type Item = &'a WaitCell<V>;
fn next(&mut self) -> Option<Self::Item> {
let ptr = self.iter.next()?.value().as_ref() as *const WaitCell<V>;
Some(unsafe { &*ptr })
}
}
pub struct Owned<K, V, S = RandomState> {
pub(crate) iter: DashOwningIter<K, Box<WaitCell<V>>, S>,
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone> Iterator for Owned<K, V, S> {
type Item = (K, Option<V>);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(key, value)| (key, value.into_inner()))
}
}