MutCursor

Struct MutCursor 

Source
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>

Source

pub fn new(root: &'root mut T) -> Self

Returns a new MutCursor with a reference to the specified root

Source

pub fn top(&self) -> &T

Returns a const reference from the mutable reference on the top of the stack

Source

pub fn top_mut(&mut self) -> &mut T

Returns the mutable reference on the top of the stack

Source

pub fn into_mut(self) -> &'root mut T

Returns the mutable reference on the top of the stack, consuming the stack

Source

pub fn try_map_into_mut<U, E, F>(self, f: F) -> Result<&'root mut U, (Self, E)>
where for<'r> F: FnOnce(&'r mut T) -> Result<&'r mut U, 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()
    }
};
Source

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

Source

pub const fn capacity(&self) -> usize

Returns the number of references the stack is capable of holding

Source

pub fn advance<F>(&mut self, step_f: F) -> bool
where F: FnOnce(&mut T) -> Option<&mut T>,

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.

Source

pub fn backtrack(&mut self)

Pops a reference from the stack, exposing the prior reference as the new top

This method will panic if the stack contains only 1 entry

Trait Implementations§

Source§

impl<'root, T: ?Sized, const N: usize> Deref for MutCursor<'root, T, N>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<'root, T: ?Sized, const N: usize> DerefMut for MutCursor<'root, T, N>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<'a, T, const N: usize> Send for MutCursor<'a, T, N>
where T: Send + ?Sized,

Source§

impl<'a, T, const N: usize> Sync for MutCursor<'a, T, N>
where T: Sync + ?Sized,

Auto Trait Implementations§

§

impl<'root, T, const N: usize> Freeze for MutCursor<'root, T, N>
where T: ?Sized,

§

impl<'root, T, const N: usize> RefUnwindSafe for MutCursor<'root, T, N>
where T: RefUnwindSafe + ?Sized,

§

impl<'root, T, const N: usize> Unpin for MutCursor<'root, T, N>
where T: ?Sized,

§

impl<'root, T, const N: usize> !UnwindSafe for MutCursor<'root, T, N>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.