Skip to main content

CursorMut

Struct CursorMut 

Source
pub struct CursorMut<'a, T, PTR: TrieIndex, LEN: TrieIndex> { /* private fields */ }
Expand description

Mutable counterpart to Cursor: a tree-walk iterator that lends out &mut T borrows over the stored values, in sorted (DFS) key order.

Unlike Cursor, the value reference is tied to &mut self (a lending cursor), not to the trie lifetime 'a. This is a soundness requirement, not a stylistic choice: a cursor is re-positionable — current(), seek(), first(), last() can all revisit a slot already visited. An 'a-tied &mut T (as the immutable cursor hands out &'a T) would let two such calls return &mut T to the same element simultaneously — aliasing undefined behavior. Tying the borrow to &mut self makes the borrow checker enforce “one live &mut T at a time,” which is the only sound rule for a re-positionable mutable cursor. The practical consequence: you cannot collect the &mut T into a Vec or hold two at once; each must be released before the next next()/prev()/current()/seek() call. In-place mutation loops (while let Some((k, v)) = c.next() { *v += 1; }) work as expected.

The key is returned as &[u8] borrowing the trie’s key buffer (zero allocation, matching the immutable cursor’s borrowed key). Both the key and the &mut T are tied to &mut self. Only the stored value is mutated; the cursor never alters key bytes, node structure, or slot occupancy, so trie invariants are preserved.

Implementations§

Source§

impl<'a, T, PTR: TrieIndex, LEN: TrieIndex> CursorMut<'a, T, PTR, LEN>

Source

pub fn new(trie: &'a mut NibTrie<T, PTR, LEN>) -> Self

Forward mutable cursor parked before the first key.

Source

pub fn new_last(trie: &'a mut NibTrie<T, PTR, LEN>) -> Self

Reverse mutable cursor parked on the last key (or empty if the trie is empty).

Source

pub fn current(&mut self) -> Option<(&[u8], &mut T)>

The key/value the cursor is parked on, or None if not parked (before first, or exhausted). The key borrows the trie’s key buffer and the &mut T reborrows the stored value — both tied to &mut self.

Three sequential, non-overlapping borrows: (1) shared peek of stack / arena to recover the key index (copied out as usize), (2) shared read of index/buf for the key slice, (3) mutable borrow of values for &mut T. buf and values are disjoint fields, so the shared key borrow and the mutable value borrow coexist.

Source

pub fn current_index(&self) -> Option<usize>

The key index the cursor is parked on, or None if not parked.

Source

pub fn first(&mut self) -> Option<(&[u8], &mut T)>

Jump to the first key (smallest in sorted order). Returns its key/value, or None if the trie is empty.

Source

pub fn last(&mut self) -> Option<(&[u8], &mut T)>

Jump to the last key (largest in sorted order). Returns its key/value, or None if the trie is empty.

Source

pub fn next(&mut self) -> Option<(&[u8], &mut T)>

Source

pub fn prev(&mut self) -> Option<(&[u8], &mut T)>

Source

pub fn next_index(&mut self) -> Option<usize>

Source

pub fn prev_index(&mut self) -> Option<usize>

Source

pub fn seek(&mut self, key: &[u8]) -> Option<(&[u8], &mut T)>

Auto Trait Implementations§

§

impl<'a, T, PTR, LEN> !UnwindSafe for CursorMut<'a, T, PTR, LEN>

§

impl<'a, T, PTR, LEN> Freeze for CursorMut<'a, T, PTR, LEN>

§

impl<'a, T, PTR, LEN> RefUnwindSafe for CursorMut<'a, T, PTR, LEN>

§

impl<'a, T, PTR, LEN> Send for CursorMut<'a, T, PTR, LEN>
where T: Send, PTR: Send, LEN: Send,

§

impl<'a, T, PTR, LEN> Sync for CursorMut<'a, T, PTR, LEN>
where T: Sync, PTR: Sync, LEN: Sync,

§

impl<'a, T, PTR, LEN> Unpin for CursorMut<'a, T, PTR, LEN>

§

impl<'a, T, PTR, LEN> UnsafeUnpin for CursorMut<'a, T, PTR, LEN>

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.