#![deny(unsafe_op_in_unsafe_fn, clippy::undocumented_unsafe_blocks)]
use std::marker::PhantomData;
use std::ops::{Bound, Deref};
use std::ptr::NonNull;
use crate::arena::node::Leaf;
use crate::arena::tree::{ArenaTree, Storage};
use crate::key::AsBytes;
use crate::raw::cursor::{Cursor, KeyBuf};
pub struct ArenaEntryRef<'a, K, V> {
leaf: NonNull<Leaf<K, V>>,
_map: PhantomData<&'a ArenaTree<K, V>>,
}
impl<K, V> Clone for ArenaEntryRef<'_, K, V> {
fn clone(&self) -> Self {
*self
}
}
impl<K, V> Copy for ArenaEntryRef<'_, K, V> {}
impl<'a, K, V> ArenaEntryRef<'a, K, V> {
#[inline]
pub(crate) unsafe fn new(leaf: NonNull<Leaf<K, V>>) -> Self {
Self {
leaf,
_map: PhantomData,
}
}
#[inline]
fn leaf(self) -> &'a Leaf<K, V> {
unsafe { self.leaf.as_ref() }
}
#[inline]
pub fn key(&self) -> &'a K {
&self.leaf().key
}
#[inline]
pub fn value(&self) -> &'a V {
&self.leaf().value
}
#[inline]
pub fn is_removed(&self) -> bool {
self.leaf().is_removed()
}
}
impl<K, V> Deref for ArenaEntryRef<'_, K, V> {
type Target = V;
#[inline]
fn deref(&self) -> &V {
self.value()
}
}
impl<K: std::fmt::Debug, V: std::fmt::Debug> std::fmt::Debug for ArenaEntryRef<'_, K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ArenaEntryRef")
.field("key", self.key())
.field("value", self.value())
.field("is_removed", &self.is_removed())
.finish()
}
}
impl<K: PartialEq, V: PartialEq> PartialEq for ArenaEntryRef<'_, K, V> {
fn eq(&self, other: &Self) -> bool {
self.key() == other.key() && self.value() == other.value()
}
}
impl<K: Eq, V: Eq> Eq for ArenaEntryRef<'_, K, V> {}
pub struct Range<'a, K, V> {
tree: &'a ArenaTree<K, V>,
cursor: Cursor<Storage<K, V>>,
}
impl<'a, K, V> Range<'a, K, V> {
pub(crate) fn new(tree: &'a ArenaTree<K, V>, start: Bound<KeyBuf>, end: Bound<KeyBuf>) -> Self {
Self {
tree,
cursor: Cursor::new(start, end),
}
}
}
impl<'a, K: AsBytes, V> Iterator for Range<'a, K, V> {
type Item = ArenaEntryRef<'a, K, V>;
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
let leaf = self.cursor.next(&self.tree.raw)?;
Some(unsafe { ArenaEntryRef::new(leaf) })
}
}
impl<K: AsBytes, V> DoubleEndedIterator for Range<'_, K, V> {
#[inline(always)]
fn next_back(&mut self) -> Option<Self::Item> {
let leaf = self.cursor.next_back(&self.tree.raw)?;
Some(unsafe { ArenaEntryRef::new(leaf) })
}
}
impl<K: AsBytes, V> std::iter::FusedIterator for Range<'_, K, V> {}