pub mod marker;
use std::{
self,
fmt::Debug,
marker::PhantomData,
ops::{Deref, DerefMut},
ptr::NonNull,
};
use crate::node::{InternalNode, LeafNode};
pub struct NodeRef<K, V, BorrowType, NodeKind> {
pointer: NonNull<InternalNode<K, V>>,
_phantom: PhantomData<(BorrowType, NodeKind)>,
}
impl<'a, K, V, Type> Copy for NodeRef<K, V, marker::Immut<'a>, Type> {}
impl<'a, K, V, Type> Clone for NodeRef<K, V, marker::Immut<'a>, Type> {
fn clone(&self) -> Self { *self }
}
impl<K, V> Debug for NodeRef<K, V, marker::Immut<'_>, marker::LeafOrInternal>
where
K: Debug,
V: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let is_internal = self.is_internal();
match is_internal {
false => {
let noderef = unsafe { self.into_leaf() };
noderef.fmt(f)
}
true => {
let noderef = unsafe { self.into_internal() };
noderef.fmt(f)
}
}
}
}
impl<K, V, BorrowType, Type> NodeRef<K, V, BorrowType, Type> {
pub fn is_internal(&self) -> bool {
unsafe { self.pointer.cast::<LeafNode<K, V>>().as_ref().is_internal() }
}
pub fn len(&self) -> usize { unsafe { self.pointer.cast::<LeafNode<K, V>>().as_ref().len() } }
}
impl<K, V, BorrowType: marker::BorrowType> NodeRef<K, V, BorrowType, marker::LeafOrInternal> {
pub unsafe fn into_internal(self) -> NodeRef<K, V, BorrowType, marker::InternalNode> {
NodeRef {
pointer: self.pointer,
_phantom: PhantomData,
}
}
pub unsafe fn into_leaf(self) -> NodeRef<K, V, BorrowType, marker::LeafNode> {
NodeRef {
pointer: self.pointer,
_phantom: PhantomData,
}
}
}
impl<K, V, BorrowType: marker::BorrowType> NodeRef<K, V, BorrowType, marker::LeafNode> {
pub fn into_type_erased(self) -> NodeRef<K, V, BorrowType, marker::LeafOrInternal> {
NodeRef {
pointer: self.pointer,
_phantom: PhantomData,
}
}
}
impl<K, V, BorrowType: marker::BorrowType> NodeRef<K, V, BorrowType, marker::InternalNode> {
pub fn into_type_erased(self) -> NodeRef<K, V, BorrowType, marker::LeafOrInternal> {
NodeRef {
pointer: self.pointer,
_phantom: PhantomData,
}
}
}
impl<K, V> NodeRef<K, V, marker::Owned, marker::LeafNode> {
pub fn from_boxed_leaf(node: Box<LeafNode<K, V>>) -> Self {
let pointer = NonNull::from(Box::leak(node)).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
pub unsafe fn into_boxed_leaf(self) -> Box<LeafNode<K, V>> {
Box::from_raw(self.pointer.as_ptr() as _)
}
}
impl<K, V> NodeRef<K, V, marker::Owned, marker::InternalNode> {
pub fn from_boxed_internal(node: Box<InternalNode<K, V>>) -> Self {
let pointer = NonNull::from(Box::leak(node)).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
pub unsafe fn into_boxed_internal(self) -> Box<InternalNode<K, V>> {
Box::from_raw(self.pointer.as_ptr() as _)
}
}
impl<K, V, Type> NodeRef<K, V, marker::Owned, Type> {
pub fn as_ref<'a>(&'a self) -> NodeRef<K, V, marker::Immut<'a>, Type> {
let pointer = self.pointer;
NodeRef {
pointer,
_phantom: PhantomData,
}
}
pub fn as_mut<'a>(&'a mut self) -> NodeRef<K, V, marker::Mut<'a>, Type> {
let pointer = self.pointer;
NodeRef {
pointer,
_phantom: PhantomData,
}
}
}
impl<'a, K, V> From<&LeafNode<K, V>> for NodeRef<K, V, marker::Immut<'a>, marker::LeafNode> {
fn from(input: &LeafNode<K, V>) -> Self {
let pointer = NonNull::from(input).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
}
impl<'a, K, V> From<&mut LeafNode<K, V>> for NodeRef<K, V, marker::Mut<'a>, marker::LeafNode> {
fn from(input: &mut LeafNode<K, V>) -> Self {
let pointer = NonNull::from(input).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
}
impl<'a, K, V> From<&mut LeafNode<K, V>> for NodeRef<K, V, marker::Immut<'a>, marker::LeafNode> {
fn from(input: &mut LeafNode<K, V>) -> Self {
let pointer = NonNull::from(input).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
}
impl<'a, K, V> From<&LeafNode<K, V>> for NodeRef<K, V, marker::Immut<'a>, marker::InternalNode> {
fn from(input: &LeafNode<K, V>) -> Self {
let pointer = NonNull::from(input).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
}
impl<'a, K, V> From<&mut LeafNode<K, V>> for NodeRef<K, V, marker::Mut<'a>, marker::InternalNode> {
fn from(input: &mut LeafNode<K, V>) -> Self {
let pointer = NonNull::from(input).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
}
impl<'a, K, V> From<&mut LeafNode<K, V>>
for NodeRef<K, V, marker::Immut<'a>, marker::InternalNode>
{
fn from(input: &mut LeafNode<K, V>) -> Self {
let pointer = NonNull::from(input).cast();
Self {
pointer,
_phantom: PhantomData,
}
}
}
impl<'a, K, V> Deref for NodeRef<K, V, marker::Immut<'a>, marker::LeafNode> {
type Target = LeafNode<K, V>;
fn deref(&self) -> &Self::Target { unsafe { self.pointer.cast().as_ref() } }
}
impl<'a, K, V> Deref for NodeRef<K, V, marker::Immut<'a>, marker::InternalNode> {
type Target = InternalNode<K, V>;
fn deref(&self) -> &Self::Target { unsafe { self.pointer.cast().as_ref() } }
}
impl<'a, K, V> Deref for NodeRef<K, V, marker::Mut<'a>, marker::LeafNode> {
type Target = LeafNode<K, V>;
fn deref(&self) -> &Self::Target { unsafe { self.pointer.cast().as_ref() } }
}
impl<'a, K, V> Deref for NodeRef<K, V, marker::Mut<'a>, marker::InternalNode> {
type Target = InternalNode<K, V>;
fn deref(&self) -> &Self::Target { unsafe { self.pointer.cast().as_ref() } }
}
impl<'a, K, V> DerefMut for NodeRef<K, V, marker::Mut<'a>, marker::LeafNode> {
fn deref_mut(&mut self) -> &mut Self::Target { unsafe { self.pointer.cast().as_mut() } }
}
impl<'a, K, V> DerefMut for NodeRef<K, V, marker::Mut<'a>, marker::InternalNode> {
fn deref_mut(&mut self) -> &mut Self::Target { unsafe { self.pointer.cast().as_mut() } }
}