MutRefStackWithData

Struct MutRefStackWithData 

Source
pub struct MutRefStackWithData<'root, T: ?Sized, U: 'root> { /* private fields */ }

Implementations§

Source§

impl<'root, T: ?Sized, U> MutRefStackWithData<'root, T, U>

Source

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

Create a new MutRefStack from a mutable reference to the root of a recursive data structure.

Examples found in repository?
examples/forest.rs (lines 17-23)
12pub fn preorder_traverse<T, F: FnMut(&mut T, usize)>(tree: &mut Forest<T>, mut callback: F) {
13    struct TraversalState {
14        next_index: usize,
15        depth: usize,
16    }
17    let mut cursor = MutRefStackWithData::new(
18        &mut *tree.roots,
19        TraversalState {
20            next_index: 0,
21            depth: 0,
22        },
23    );
24    loop {
25        let Ok(_) = cursor.move_with(|element, state| {
26            if state.next_index >= element.len() {
27                MoveDecision::Ascend
28            } else {
29                callback(&mut element[state.next_index].data, state.depth);
30                let decision = MoveDecision::Descend(
31                    &mut *element[state.next_index].children,
32                    TraversalState {
33                        next_index: 0,
34                        depth: state.depth + 1,
35                    },
36                );
37                state.next_index += 1;
38                decision
39            }
40        }) else {
41            break;
42        };
43    }
44}
Source

pub fn top(&self) -> (&T, &U)

Obtain a shared reference to the top of the stack.

Source

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

Obtain a mutable reference to the top of the stack.

Source

pub fn is_at_root(&self) -> bool

Is this MutRefStack currently at its root?

Source

pub fn descend_with( &mut self, f: impl for<'node, 'addl> FnOnce(&'node mut T, &'addl mut U) -> Option<(&'node mut T, U)>, ) -> Option<(&mut T, &mut U)>

Descend into the recursive data structure, returning a mutable reference to the new top element. Rust’s borrow checker enforces that the closure cannot inject any lifetime (other than 'static), because the closure must work for any lifetime 'node.

Source

pub fn inject_with( &mut self, f: impl for<'node, 'addl> FnOnce(&'node mut T, &'addl mut U) -> Option<(&'root mut T, U)>, ) -> Option<(&mut T, &mut U)>

Inject a new reference to the top of the stack. The reference still must live as long as the root of the stack.

Source

pub fn ascend(&mut self) -> Option<((&mut T, &mut U), U)>

Ascend back up from the recursive data structure, returning a mutable reference to the new top element, if it changed. If we are not currently at the root, ascend and return a reference to the new top, a reference to the new top’s additional data, and the old top’s additional data. If we are already the root, returns None (the top is the root and does not change).

Source

pub fn ascend_while<P>( &mut self, predicate: P, ) -> ((&mut T, &mut U), impl IntoIterator<Item = U>)
where P: FnMut(&mut T, &mut U) -> bool,

Ascend back up from the recursive data structure while the given closure returns true, returning a mutable reference to the new top element. If we are not currently at the root, and the predicate returns true, ascend and continue. If we are already at the root, or if the predicate returned false, returns a reference to the top element.

Source

pub fn move_with( &mut self, f: impl for<'node, 'addl> FnOnce(&'node mut T, &'addl mut U) -> MoveDecision<'root, 'node, T, U>, ) -> Result<((&mut T, &mut U), Option<U>), MoveError>

Ascend from, descend from, inject a new top, or stay at the current node, based on the return value of the closure.

Examples found in repository?
examples/forest.rs (lines 25-40)
12pub fn preorder_traverse<T, F: FnMut(&mut T, usize)>(tree: &mut Forest<T>, mut callback: F) {
13    struct TraversalState {
14        next_index: usize,
15        depth: usize,
16    }
17    let mut cursor = MutRefStackWithData::new(
18        &mut *tree.roots,
19        TraversalState {
20            next_index: 0,
21            depth: 0,
22        },
23    );
24    loop {
25        let Ok(_) = cursor.move_with(|element, state| {
26            if state.next_index >= element.len() {
27                MoveDecision::Ascend
28            } else {
29                callback(&mut element[state.next_index].data, state.depth);
30                let decision = MoveDecision::Descend(
31                    &mut *element[state.next_index].children,
32                    TraversalState {
33                        next_index: 0,
34                        depth: state.depth + 1,
35                    },
36                );
37                state.next_index += 1;
38                decision
39            }
40        }) else {
41            break;
42        };
43    }
44}
Source

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

Return reference to the top element of this stack, forgetting about the stack entirely.

Auto Trait Implementations§

§

impl<'root, T, U> Freeze for MutRefStackWithData<'root, T, U>
where T: ?Sized,

§

impl<'root, T, U> RefUnwindSafe for MutRefStackWithData<'root, T, U>

§

impl<'root, T, U> !Send for MutRefStackWithData<'root, T, U>

§

impl<'root, T, U> !Sync for MutRefStackWithData<'root, T, U>

§

impl<'root, T, U> Unpin for MutRefStackWithData<'root, T, U>
where U: Unpin, T: ?Sized,

§

impl<'root, T, U> !UnwindSafe for MutRefStackWithData<'root, T, U>

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