pub struct CursorMut<'a, K: TrieKey, V> { /* private fields */ }Expand description
Mutable counterpart to Cursor: a tree-walk iterator that lends out
&mut V 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 V (as the immutable cursor hands out &'a V) would let two such
calls return &mut V to the same element simultaneously — aliasing
undefined behavior. Tying the borrow to &mut self makes the borrow checker
enforce “one live &mut V at a time,” which is the only sound rule for a
re-positionable mutable cursor. The practical consequence: you cannot
collect the &mut V 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 store (zero
allocation, matching the immutable cursor’s borrowed key). Both the key and
the &mut V 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, K: TrieKey, V> CursorMut<'a, K, V>
impl<'a, K: TrieKey, V> CursorMut<'a, K, V>
Sourcepub fn new(trie: &'a mut BitTrie<K, V>) -> Self
pub fn new(trie: &'a mut BitTrie<K, V>) -> Self
Forward mutable cursor parked before the first key.
Sourcepub fn new_last(trie: &'a mut BitTrie<K, V>) -> Self
pub fn new_last(trie: &'a mut BitTrie<K, V>) -> Self
Reverse mutable cursor parked on the last key (or empty if the trie is empty).
Sourcepub fn current(&mut self) -> Option<(&[u8], &mut V)>
pub fn current(&mut self) -> Option<(&[u8], &mut V)>
The key/value the cursor is parked on, or None if not parked (before
first, or exhausted). The key borrows the trie’s key store and the
&mut V reborrows the stored value — both tied to &mut self.
keys and values are disjoint fields of the trie, so the shared key
borrow (via key_bytes) and the mutable value borrow coexist without
aliasing.
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 V)>
pub fn first(&mut self) -> Option<(&[u8], &mut V)>
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 V)>
pub fn last(&mut self) -> Option<(&[u8], &mut V)>
Jump to the last key (largest in sorted order). Returns its key/value,
or None if the trie is empty.