use super::{Node, RbTree};
use crate::label::Label;
use crate::AsHashTree;
use std::fmt::{self, Debug};
use Entry::{Occupied, Vacant};
pub enum Entry<'a, K: 'static + Label, V: AsHashTree + 'static> {
Vacant(VacantEntry<'a, K, V>),
Occupied(OccupiedEntry<'a, K, V>),
}
pub struct VacantEntry<'a, K: 'static + Label, V: AsHashTree + 'static> {
pub(super) map: &'a mut RbTree<K, V>,
pub(super) key: K,
}
pub struct OccupiedEntry<'a, K: 'static + Label, V: AsHashTree + 'static> {
pub(super) map: &'a mut RbTree<K, V>,
pub(super) key: K,
pub(super) node: *mut Node<K, V>,
}
impl<'a, K: 'static + Label, V: AsHashTree + 'static> VacantEntry<'a, K, V> {
#[inline]
pub fn insert(self, value: V) -> &'a mut V {
self.map.insert(self.key, value).1
}
#[inline]
pub fn into_key(self) -> K {
self.key
}
#[inline]
pub fn key(&self) -> &K {
&self.key
}
}
impl<'a, K: 'static + Label, V: AsHashTree + 'static> OccupiedEntry<'a, K, V> {
#[inline]
pub fn get(&self) -> &V {
unsafe { &(*self.node).value }
}
#[inline]
pub fn get_mut(&mut self) -> &mut V {
unsafe { &mut (*self.node).value }
}
#[inline]
pub fn into_mut(self) -> &'a mut V {
unsafe { &mut (*self.node).value }
}
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn remove(self) -> V {
self.map.delete(&self.key).unwrap().1
}
#[inline]
pub fn remove_entry(self) -> (K, V) {
self.map.delete(&self.key).unwrap()
}
}
impl<'a, K: 'static + Label, V: AsHashTree + 'static> Entry<'a, K, V> {
#[inline]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Occupied(mut entry) => {
f(entry.get_mut());
Occupied(entry)
}
Vacant(entry) => Vacant(entry),
}
}
#[inline]
pub fn or_default(self) -> &'a mut V
where
V: Default,
{
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(Default::default()),
}
}
#[inline]
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default),
}
}
#[inline]
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default()),
}
}
#[inline]
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => {
let value = default(entry.key());
entry.insert(value)
}
}
}
#[inline]
pub fn key(&self) -> &K {
match self {
Occupied(entry) => entry.key(),
Vacant(entry) => entry.key(),
}
}
}
impl<'a, K: 'static + Label, V: AsHashTree + 'static> Debug for Entry<'a, K, V>
where
K: Debug,
V: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Vacant(ref v) => f.debug_tuple("Entry").field(v).finish(),
Occupied(ref o) => f.debug_tuple("Entry").field(o).finish(),
}
}
}
impl<'a, K: 'static + Label, V: AsHashTree + 'static> Debug for VacantEntry<'a, K, V>
where
K: Debug,
V: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("VacantEntry").field(self.key()).finish()
}
}
impl<'a, K: 'static + Label, V: AsHashTree + 'static> Debug for OccupiedEntry<'a, K, V>
where
K: Debug,
V: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntry")
.field("key", self.key())
.field("value", self.get())
.finish()
}
}