use super::ReadGuard;
use crate::inner::Inner;
use one_way_slot_map::{SlotMapKey as Key, SlotMapKeyData};
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use super::user_friendly;
#[derive(Debug)]
pub struct MapReadRef<'rh, K, P, V>
where
K: Key<P>,
{
pub(super) guard: ReadGuard<'rh, Inner<ManuallyDrop<V>>>,
pub(super) _phantom_k: PhantomData<K>,
pub(super) _phantom_p: PhantomData<P>,
}
impl<'rh, K, P, V> MapReadRef<'rh, K, P, V>
where
K: Key<P>,
{
pub fn len(&self) -> usize {
self.guard.data.len()
}
pub fn is_empty(&self) -> bool {
self.guard.data.is_empty()
}
pub fn values(&self) -> impl Iterator<Item = &V> {
self.guard.data.values().map(user_friendly)
}
pub fn iter<F>(
&self,
mut pointer_finder: F,
) -> impl Iterator<Item = (K, &V)>
where
F: FnMut(&V) -> P,
{
self.iter_raw().map(move |(key_data, v)| {
(K::from(((&mut pointer_finder)(v), key_data)), v)
})
}
pub fn iter_raw(&self) -> impl Iterator<Item = (SlotMapKeyData, &V)> {
self.guard
.data
.iter_raw()
.map(|(key_data, v)| (key_data, user_friendly(v)))
}
pub fn get<'a>(&'a self, key: &'_ K) -> Option<&'a V> {
self.guard.data.get_unbounded(key).map(user_friendly)
}
pub fn contains_key(&self, key: &K) -> bool {
self.guard.data.contains_key_unbounded(key)
}
}