pub struct AvlSearchResult<'a, P: Pointer> {
pub node: *const P::Target,
pub direction: Option<AvlDirection>,
/* private fields */
}avl only.Expand description
Result of a search operation in an AvlTree.
An AvlSearchResult identifies either:
- An exact match:
directionisNoneandnodepoints to the matching item. - An insertion point:
directionisSome(dir)andnodepoints to the parent where a new node should be attached as thedirchild.
The lifetime 'a ties the search result to the tree’s borrow, ensuring safety.
However, this lifetime often prevents further mutable operations on the tree
(e.g., adding a node while holding the search result). Use detach
to de-couple the result from the tree’s lifetime when necessary.
Fields§
§node: *const P::TargetThe matching node or the parent for insertion.
direction: Option<AvlDirection>None if exact match found, or Some(direction) indicating insertion point.
Implementations§
Source§impl<'a, P: Pointer> AvlSearchResult<'a, P>
impl<'a, P: Pointer> AvlSearchResult<'a, P>
Sourcepub fn get_node_ref(&self) -> Option<&'a P::Target>
pub fn get_node_ref(&self) -> Option<&'a P::Target>
Returns a reference to the matching node if the search was an exact match.
Sourcepub unsafe fn detach<'b>(&'a self) -> AvlSearchResult<'b, P>
pub unsafe fn detach<'b>(&'a self) -> AvlSearchResult<'b, P>
De-couple the lifetime of the search result from the tree.
This method is essential for performing mutable operations on the tree
using search results. In Rust, a search result typically borrows the tree
immutably. If you need to modify the tree (e.g., call insert or remove)
based on that result, the borrow checker would normally prevent it.
detach effectively “erases” the lifetime 'a, returning a result with
an unbounded lifetime 'b.
§Examples
Used in RangeTree::add:
let result = self.root.find(&rs_key, range_tree_segment_cmp);
// result is AvlSearchResult<'a, ...> and borrows self.root
let detached = unsafe { result.detach() };
// detached has no lifetime bound to self.root
self.space += size; // Mutable operation on self permitted
self.merge_seg(start, end, detached); // Mutation on tree permitted§Safety
This is an unsafe operation. The compiler no longer protects the validity
of the internal pointer via lifetimes. You must ensure that the tree
structure is not modified in a way that invalidates node (e.g., the
parent node being removed) before using the detached result.
Sourcepub fn get_nearest(&self) -> Option<&P::Target>
pub fn get_nearest(&self) -> Option<&P::Target>
Return the nearest node in the search result