use crossbeam_epoch::Guard;
use std::ops::Deref;
use crate::key::AsBytes;
use crate::tree::Tree;
pub struct EntryRef<'a, K: AsBytes + Send + 'static, V: Send + 'static> {
pub(crate) key_ptr: *const K,
pub(crate) val_ptr: *const V,
pub(crate) tree: &'a Tree<K, V>,
pub(crate) guard: &'a Guard,
pub(crate) is_removed: bool,
}
impl<'a, K: AsBytes + Send + 'static, V: Send + 'static> EntryRef<'a, K, V> {
#[inline]
pub fn key(&self) -> &K {
unsafe { &*self.key_ptr }
}
#[inline]
pub fn value(&self) -> &V {
unsafe { &*self.val_ptr }
}
#[inline]
pub fn is_removed(&self) -> bool {
self.is_removed
}
pub fn remove(&mut self) -> bool {
if self.is_removed {
return false;
}
let key = self.key();
let removed = self.tree.remove(key, self.guard).is_some();
if removed {
self.is_removed = true;
}
removed
}
}
impl<'a, K: AsBytes + Send + 'static, V: Send + 'static> Deref for EntryRef<'a, K, V> {
type Target = V;
#[inline]
fn deref(&self) -> &Self::Target {
self.value()
}
}