pub struct MutRefStackWithData<'root, T: ?Sized, U: 'root> { /* private fields */ }Implementations§
Source§impl<'root, T: ?Sized, U> MutRefStackWithData<'root, T, U>
impl<'root, T: ?Sized, U> MutRefStackWithData<'root, T, U>
Sourcepub fn new(root: &'root mut T, additional_data: U) -> Self
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?
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}Sourcepub fn top_mut(&mut self) -> (&mut T, &mut U)
pub fn top_mut(&mut self) -> (&mut T, &mut U)
Obtain a mutable reference to the top of the stack.
Sourcepub fn is_at_root(&self) -> bool
pub fn is_at_root(&self) -> bool
Is this MutRefStack currently at its root?
Sourcepub 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)>
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.
Sourcepub 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)>
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.
Sourcepub fn ascend(&mut self) -> Option<((&mut T, &mut U), U)>
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).
Sourcepub fn ascend_while<P>(
&mut self,
predicate: P,
) -> ((&mut T, &mut U), impl IntoIterator<Item = U>)
pub fn ascend_while<P>( &mut self, predicate: P, ) -> ((&mut T, &mut U), impl IntoIterator<Item = U>)
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.
Sourcepub 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>
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?
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}Sourcepub fn into_top(self) -> &'root mut T
pub fn into_top(self) -> &'root mut T
Return reference to the top element of this stack, forgetting about the stack entirely.