pub struct MutCursor<'root, T: ?Sized + 'root, const N: usize> { /* private fields */ }Expand description
Stores a stack of &mut references, only allowing access to the top element on the stack
The MutCursor stores N &mut T references, but only allows access to the top
Implementations§
Source§impl<'root, T: ?Sized + 'root, const N: usize> MutCursor<'root, T, N>
impl<'root, T: ?Sized + 'root, const N: usize> MutCursor<'root, T, N>
Sourcepub fn new(root: &'root mut T) -> Self
pub fn new(root: &'root mut T) -> Self
Returns a new MutCursor with a reference to the specified root
Sourcepub fn top(&self) -> &T
pub fn top(&self) -> &T
Returns a const reference from the mutable reference on the top of the stack
Sourcepub fn into_mut(self) -> &'root mut T
pub fn into_mut(self) -> &'root mut T
Returns the mutable reference on the top of the stack, consuming the stack
Sourcepub fn try_map_into_mut<U, E, F>(self, f: F) -> Result<&'root mut U, (Self, E)>
pub fn try_map_into_mut<U, E, F>(self, f: F) -> Result<&'root mut U, (Self, E)>
Consumes the stack and returns a mutable reference to an object with the 'root lifetime,
if a closure returns Ok, otherwise returns the stack and a custom error value
This method is useful when you need to call a fallible API with the node, but need the result
of the API to be in the 'root lifetime so it can outlive the MutCursor.
use mutcursor::MutCursor;
let mut tree = TreeNode::new(3);
let node_stack = MutCursor::<TreeNode, 2>::new(&mut tree);
let node_ref = match node_stack.try_map_into_mut(|top_ref| {
if top_ref.is_leaf() {
Ok(top_ref)
} else {
Err(top_ref.val)
}
}) {
Ok(node) => node,
Err((mut node_stack, _val)) => {
if node_stack.depth() > 0 {
node_stack.backtrack();
}
node_stack.into_mut()
}
};Sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Returns the number of excess references stored in the stack, which corresponds to the number of times backtrack may be called
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the number of references the stack is capable of holding
Sourcepub fn advance<F>(&mut self, step_f: F) -> bool
pub fn advance<F>(&mut self, step_f: F) -> bool
Steps deeper into the traversal, pushing a new reference onto the top of the stack
If the step_f closure returns Some(), the contained reference is pushed onto the stack and
this method returns true. If the closure returns None then the stack is unmodified and this
method returns false.
If the number of references in the stack exceeds the capacity, the reference at the bottom of the stack will be lost.