Skip to main content

AvlSearchResult

Struct AvlSearchResult 

Source
pub struct AvlSearchResult<'a, P: Pointer> {
    pub node: *const P::Target,
    pub direction: Option<AvlDirection>,
    /* private fields */
}
Available on crate feature avl only.
Expand description

Result of a search operation in an AvlTree.

An AvlSearchResult identifies either:

  1. An exact match: direction is None and node points to the matching item.
  2. An insertion point: direction is Some(dir) and node points to the parent where a new node should be attached as the dir child.

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::Target

The 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>

Source

pub fn get_node_ref(&self) -> Option<&'a P::Target>

Returns a reference to the matching node if the search was an exact match.

Source

pub fn is_exact(&self) -> bool

Returns true if the search result is an exact match.

Source

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.

Source

pub fn get_nearest(&self) -> Option<&P::Target>

Return the nearest node in the search result

Source§

impl<'a, T> AvlSearchResult<'a, Arc<T>>

Source

pub fn get_exact(&self) -> Option<Arc<T>>

Returns the matching Arc node if this is an exact match.

Source§

impl<'a, T> AvlSearchResult<'a, Rc<T>>

Source

pub fn get_exact(&self) -> Option<Rc<T>>

Returns the matching Rc node if this is an exact match.

Trait Implementations§

Source§

impl<P: Pointer> Default for AvlSearchResult<'_, P>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, P> Freeze for AvlSearchResult<'a, P>

§

impl<'a, P> RefUnwindSafe for AvlSearchResult<'a, P>
where <P as Pointer>::Target: RefUnwindSafe,

§

impl<'a, P> !Send for AvlSearchResult<'a, P>

§

impl<'a, P> !Sync for AvlSearchResult<'a, P>

§

impl<'a, P> Unpin for AvlSearchResult<'a, P>

§

impl<'a, P> UnwindSafe for AvlSearchResult<'a, P>
where <P as Pointer>::Target: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.