refactor HandleOrTree to generic TreeOr type

This commit is contained in:
janis 2025-09-16 19:22:08 +02:00
parent 96e7045d5b
commit 3e3b096174
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -725,7 +725,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V>, marker::Edge> {
// search: // search:
mod search { mod search {
use super::{marker, ForceResult, Handle, NodeRef}; use super::{ForceResult, Handle, NodeRef, marker};
use core::borrow::Borrow; use core::borrow::Borrow;
use core::cmp::Ordering; use core::cmp::Ordering;
@ -864,6 +864,14 @@ mod search {
} }
} }
enum TreeOr<'a, K, V, T> {
Tree(borrow::DormantMutRef<'a, Tree<K, V>>),
Other(T),
}
type TreeOrHandle<'a, BorrowType, K, V, HandleType> =
TreeOr<'a, K, V, Handle<NodeRef<BorrowType, K, V>, HandleType>>;
enum HandleOrTree<'a, BorrowType, K, V, HandleType> { enum HandleOrTree<'a, BorrowType, K, V, HandleType> {
Handle(Handle<NodeRef<BorrowType, K, V>, HandleType>), Handle(Handle<NodeRef<BorrowType, K, V>, HandleType>),
Tree(borrow::DormantMutRef<'a, Tree<K, V>>), Tree(borrow::DormantMutRef<'a, Tree<K, V>>),
@ -872,7 +880,9 @@ enum HandleOrTree<'a, BorrowType, K, V, HandleType> {
mod entry { mod entry {
use core::marker::PhantomData; use core::marker::PhantomData;
use super::{marker, Handle, NodeRef}; use crate::tree::TreeOrHandle;
use super::{Handle, NodeRef, marker};
pub enum Entry<'a, Q: 'a, K: 'a, V: 'a> pub enum Entry<'a, Q: 'a, K: 'a, V: 'a>
where where
@ -922,7 +932,7 @@ mod entry {
Q: Iterator<Item = K>, Q: Iterator<Item = K>,
{ {
pub(super) key: Q, pub(super) key: Q,
pub(super) handle: super::HandleOrTree<'a, marker::Mut<'a>, K, V, marker::Edge>, pub(super) handle: super::TreeOrHandle<'a, marker::Mut<'a>, K, V, marker::Edge>,
pub(super) _marker: PhantomData<&'a mut (K, V)>, pub(super) _marker: PhantomData<&'a mut (K, V)>,
} }
@ -963,10 +973,9 @@ mod entry {
Q: Iterator<Item = K>, Q: Iterator<Item = K>,
{ {
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> { pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
use super::HandleOrTree;
let handle = match self.handle { let handle = match self.handle {
// no root node yet // no root node yet
HandleOrTree::Tree(mut tree) => { TreeOrHandle::Tree(mut tree) => {
// SAFETY: there are no nodes in the tree yet // SAFETY: there are no nodes in the tree yet
let tree = unsafe { tree.reborrow() }; let tree = unsafe { tree.reborrow() };
let root = tree.root.insert(NodeRef::new()); let root = tree.root.insert(NodeRef::new());
@ -975,7 +984,7 @@ mod entry {
Handle::new_edge(root.borrow_mut(), 0).insert_recursing(self.key, value) Handle::new_edge(root.borrow_mut(), 0).insert_recursing(self.key, value)
} }
} }
HandleOrTree::Handle(handle) => unsafe { handle.insert_recursing(self.key, value) }, TreeOrHandle::Other(handle) => unsafe { handle.insert_recursing(self.key, value) },
}; };
OccupiedEntry { OccupiedEntry {
@ -989,11 +998,12 @@ mod entry {
mod subtree { mod subtree {
use core::marker::PhantomData; use core::marker::PhantomData;
use crate::tree::{search, HandleOrTree}; use crate::tree::{TreeOrHandle, search};
use super::{ use super::{
NodeRef, OnceAndIter,
entry::{Entry, OccupiedEntry, VacantEntry}, entry::{Entry, OccupiedEntry, VacantEntry},
marker, NodeRef, OnceAndIter, marker,
}; };
// BorrowType may be one of `Immut`, `Mut`. // BorrowType may be one of `Immut`, `Mut`.
@ -1085,12 +1095,12 @@ mod subtree {
}), }),
search::SearchResult::GoDown(handle) => Vacant(VacantEntry { search::SearchResult::GoDown(handle) => Vacant(VacantEntry {
key: key_seq.into(), key: key_seq.into(),
handle: HandleOrTree::Handle(handle), handle: TreeOrHandle::Other(handle),
_marker: PhantomData, _marker: PhantomData,
}), }),
search::SearchResult::Insert(key, handle) => Vacant(VacantEntry { search::SearchResult::Insert(key, handle) => Vacant(VacantEntry {
key: OnceAndIter::once(key, key_seq), key: OnceAndIter::once(key, key_seq),
handle: HandleOrTree::Handle(handle), handle: TreeOrHandle::Other(handle),
_marker: PhantomData, _marker: PhantomData,
}), }),
} }
@ -1330,7 +1340,7 @@ where
} }
None => Vacant(VacantEntry { None => Vacant(VacantEntry {
key: key_seq.into(), key: key_seq.into(),
handle: HandleOrTree::Tree(dormant), handle: TreeOrHandle::Tree(dormant),
_marker: PhantomData, _marker: PhantomData,
}), }),
}; };