pub struct CursorMut<'a, K: ByteKey, T, PTR: TrieIndex, LEN: TrieIndex> { /* private fields */ }Expand description
Mutable counterpart to Cursor: a linear scan of the sparse index
that lends out &mut T borrows over the stored values.
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 &'a 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 ByteKey::Borrowed<'_> (via ByteKey::as_borrowed)
— a zero-alloc view into the trie’s key buffer, tied to the same &mut self
borrow as the &mut T (so it, too, must be released before the next call).
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, K: ByteKey, T, PTR: TrieIndex, LEN: TrieIndex> CursorMut<'a, K, T, PTR, LEN>
impl<'a, K: ByteKey, T, PTR: TrieIndex, LEN: TrieIndex> CursorMut<'a, K, T, PTR, LEN>
Sourcepub fn new(trie: &'a mut NibbleTrie<K, T, PTR, LEN>) -> Self
pub fn new(trie: &'a mut NibbleTrie<K, T, PTR, LEN>) -> Self
Forward mutable cursor parked before the first key.
Sourcepub fn new_last(trie: &'a mut NibbleTrie<K, T, PTR, LEN>) -> Self
pub fn new_last(trie: &'a mut NibbleTrie<K, T, PTR, LEN>) -> Self
Reverse mutable cursor parked on the last key (or before-first if empty).
Sourcepub fn first(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
pub fn first(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
Jump to the first key (smallest slot). Returns its key/value, or None
if the trie is empty. Scans forward from slot 1.
Sourcepub fn last(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
pub fn last(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
Jump to the last key (largest slot). Returns its key/value, or None if
the trie is empty. Scans backward from the end of index.
Sourcepub fn current(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
pub fn current(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
The key/value the cursor is parked on, or None if not parked (before
first, or exhausted). Reconstructs K and reborrows &mut T per call.
Sourcepub fn current_index(&self) -> Option<usize>
pub fn current_index(&self) -> Option<usize>
The slot index the cursor is parked on, or None if not parked.
Sourcepub fn next(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
pub fn next(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
Advance to the next occupied slot and return its key/value. Returns
None (parking at the forward-exhausted sentinel) when no further key
exists.
Sourcepub fn prev(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
pub fn prev(&mut self) -> Option<(K::Borrowed<'_>, &mut T)>
Step to the previous occupied slot and return its key/value. Returns
None (parking at the before-first sentinel) when no prior key exists.