pub struct OccupiedEntry<'a, K, V> {
key: K,
entry: &'a mut Option<V>,
}
pub struct VacantEntry<'a, K, V> {
key: K,
entry: &'a mut Option<V>,
}
pub enum Entry<'a, K, V> {
Occupied(OccupiedEntry<'a, K, V>),
Vacant(VacantEntry<'a, K, V>),
}
impl<'a, K, V> OccupiedEntry<'a, K, V> {
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn get(&self) -> &V {
self.entry.as_ref().unwrap()
}
#[inline]
pub fn get_mut(&mut self) -> &mut V {
self.entry.as_mut().unwrap()
}
}
impl<'a, K, V> VacantEntry<'a, K, V> {
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn insert(self, value: V) {
*self.entry = Some(value);
}
}
impl<'a, K, V> Entry<'a, K, V> {
pub(crate) fn new(key: K, entry: &'a mut Option<V>) -> Self {
if entry.is_some() {
Entry::Occupied(OccupiedEntry { key, entry })
} else {
Entry::Vacant(VacantEntry { key, entry })
}
}
#[inline]
pub fn key(&self) -> &K {
match self {
Entry::Occupied(entry) => entry.key(),
Entry::Vacant(entry) => entry.key(),
}
}
#[inline]
pub fn or_insert(self, value: V) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.entry.as_mut().unwrap(),
Entry::Vacant(entry) => entry.entry.insert(value),
}
}
}