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>
impl<'a, T, PTR: TrieIndex, LEN: TrieIndex> CursorMut<'a, T, PTR, LEN>
Sourcepub fn new(trie: &'a mut NibTrie<T, PTR, LEN>) -> Self
pub fn new(trie: &'a mut NibTrie<T, PTR, LEN>) -> Self
Forward mutable cursor parked before the first key.
Sourcepub fn new_last(trie: &'a mut NibTrie<T, PTR, LEN>) -> Self
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).
Sourcepub fn current(&mut self) -> Option<(&[u8], &mut T)>
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.
Sourcepub fn current_index(&self) -> Option<usize>
pub fn current_index(&self) -> Option<usize>
The key index the cursor is parked on, or None if not parked.
Sourcepub fn first(&mut self) -> Option<(&[u8], &mut T)>
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.
Sourcepub fn last(&mut self) -> Option<(&[u8], &mut T)>
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.