use std::ops::Deref;
use std::sync::atomic::Ordering;
use crate::key::AsBytes;
use crate::node::Leaf;
use crate::tree::Tree;
pub struct EntryRef<'a, K: AsBytes + Send + 'static, V: Send + 'static> {
pub(crate) leaf_ptr: *mut Leaf<K, V>,
pub(crate) tree: &'a Tree<K, V>,
}
impl<'a, K: AsBytes + Send + 'static, V: Send + 'static> Clone for EntryRef<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
leaf_ptr: self.leaf_ptr,
tree: self.tree,
}
}
}
impl<'a, K: AsBytes + Send + 'static, V: Send + 'static> EntryRef<'a, K, V> {
#[inline]
pub fn key(&self) -> &'a K {
unsafe { &(*self.leaf_ptr).key }
}
#[inline]
pub fn value(&self) -> &'a V {
unsafe { &(*self.leaf_ptr).value }
}
#[inline]
pub fn is_removed(&self) -> bool {
unsafe { (*self.leaf_ptr).removed.load(Ordering::Acquire) }
}
#[inline]
pub fn remove(&self) -> bool {
let guard = &crossbeam_epoch::pin();
self.tree.remove_leaf(self.leaf_ptr, guard)
}
}
impl<'a, K: AsBytes + Send + 'static + std::fmt::Debug, V: Send + 'static + std::fmt::Debug>
std::fmt::Debug for EntryRef<'a, K, V>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EntryRef")
.field("key", self.key())
.field("value", self.value())
.field("is_removed", &self.is_removed())
.finish()
}
}
impl<'a, K: AsBytes + Send + 'static + PartialEq, V: Send + 'static + PartialEq> PartialEq
for EntryRef<'a, K, V>
{
fn eq(&self, other: &Self) -> bool {
self.key() == other.key() && self.value() == other.value()
}
}
impl<'a, K: AsBytes + Send + 'static + Eq, V: Send + 'static + Eq> Eq for EntryRef<'a, K, V> {}
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()
}
}