RefCellRefMutStack

Struct RefCellRefMutStack 

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

Implementations§

Source§

impl<'root, T: ?Sized> RefCellRefMutStack<'root, T>

Source

pub fn new(root: &'root RefCell<T>) -> Result<Self, BorrowMutError>

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

Examples found in repository?
examples/cyclic.rs (line 43)
23fn main() {
24    let cycle_a = Rc::new(RefCell::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let cycle_b = Rc::new(RefCell::new(CyclicDataStructure {
29        data: 1_u32,
30        next: None,
31    }));
32    let cycle_c = Rc::new(RefCell::new(CyclicDataStructure {
33        data: 2_u32,
34        next: None,
35    }));
36
37    cycle_a.borrow_mut().insert_next(cycle_b.clone());
38    cycle_b.borrow_mut().insert_next(cycle_c.clone());
39    cycle_c.borrow_mut().insert_next(cycle_a.clone());
40
41    // Using a MutRefStack to descend *and then ascend* the data structure.
42    // This cannot be done with regular mutable references.
43    let mut stack = RefCellRefMutStack::new(&cycle_a).expect("not mutable borrowed yet");
44    println!("Stack currently at item with value: {}", stack.top().data);
45    loop {
46        if let Err(_borrow_error) = stack
47            .descend_with(CyclicDataStructure::next)
48            .expect("no node has no next")
49        {
50            println!("Found a cycle!");
51            break;
52        }
53        println!("Descended successfully!");
54        println!("Stack currently at item with value: {}", stack.top().data);
55    }
56    println!("Stack currently at item with value: {}", stack.top().data);
57    loop {
58        if let None = stack.ascend() {
59            println!("Reached the head of the linked list!");
60            break;
61        }
62        println!("Ascended successfully!");
63        println!("Stack currently at item with value: {}", stack.top().data);
64    }
65
66    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
67    stack.top_mut().take_next();
68}
Source

pub fn raw_top_mut(&mut self) -> *mut T

Source

pub fn top(&self) -> &T

Obtain a shared reference to the top of the stack.

Examples found in repository?
examples/cyclic.rs (line 44)
23fn main() {
24    let cycle_a = Rc::new(RefCell::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let cycle_b = Rc::new(RefCell::new(CyclicDataStructure {
29        data: 1_u32,
30        next: None,
31    }));
32    let cycle_c = Rc::new(RefCell::new(CyclicDataStructure {
33        data: 2_u32,
34        next: None,
35    }));
36
37    cycle_a.borrow_mut().insert_next(cycle_b.clone());
38    cycle_b.borrow_mut().insert_next(cycle_c.clone());
39    cycle_c.borrow_mut().insert_next(cycle_a.clone());
40
41    // Using a MutRefStack to descend *and then ascend* the data structure.
42    // This cannot be done with regular mutable references.
43    let mut stack = RefCellRefMutStack::new(&cycle_a).expect("not mutable borrowed yet");
44    println!("Stack currently at item with value: {}", stack.top().data);
45    loop {
46        if let Err(_borrow_error) = stack
47            .descend_with(CyclicDataStructure::next)
48            .expect("no node has no next")
49        {
50            println!("Found a cycle!");
51            break;
52        }
53        println!("Descended successfully!");
54        println!("Stack currently at item with value: {}", stack.top().data);
55    }
56    println!("Stack currently at item with value: {}", stack.top().data);
57    loop {
58        if let None = stack.ascend() {
59            println!("Reached the head of the linked list!");
60            break;
61        }
62        println!("Ascended successfully!");
63        println!("Stack currently at item with value: {}", stack.top().data);
64    }
65
66    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
67    stack.top_mut().take_next();
68}
Source

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

Obtain a mutable reference to the top of the stack.

Examples found in repository?
examples/cyclic.rs (line 67)
23fn main() {
24    let cycle_a = Rc::new(RefCell::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let cycle_b = Rc::new(RefCell::new(CyclicDataStructure {
29        data: 1_u32,
30        next: None,
31    }));
32    let cycle_c = Rc::new(RefCell::new(CyclicDataStructure {
33        data: 2_u32,
34        next: None,
35    }));
36
37    cycle_a.borrow_mut().insert_next(cycle_b.clone());
38    cycle_b.borrow_mut().insert_next(cycle_c.clone());
39    cycle_c.borrow_mut().insert_next(cycle_a.clone());
40
41    // Using a MutRefStack to descend *and then ascend* the data structure.
42    // This cannot be done with regular mutable references.
43    let mut stack = RefCellRefMutStack::new(&cycle_a).expect("not mutable borrowed yet");
44    println!("Stack currently at item with value: {}", stack.top().data);
45    loop {
46        if let Err(_borrow_error) = stack
47            .descend_with(CyclicDataStructure::next)
48            .expect("no node has no next")
49        {
50            println!("Found a cycle!");
51            break;
52        }
53        println!("Descended successfully!");
54        println!("Stack currently at item with value: {}", stack.top().data);
55    }
56    println!("Stack currently at item with value: {}", stack.top().data);
57    loop {
58        if let None = stack.ascend() {
59            println!("Reached the head of the linked list!");
60            break;
61        }
62        println!("Ascended successfully!");
63        println!("Stack currently at item with value: {}", stack.top().data);
64    }
65
66    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
67    stack.top_mut().take_next();
68}
Source

pub fn is_at_root(&self) -> bool

Is this MutRefStack currently at its root?

Source

pub fn inject_top( &mut self, new_top: &'root RefCell<T>, ) -> Result<&mut T, BorrowMutError>

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 inject_with( &mut self, f: impl FnOnce(&mut T) -> Option<&'root RefCell<T>>, ) -> Option<Result<&mut T, BorrowMutError>>

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 descend_with( &mut self, f: impl for<'node> FnOnce(&'node mut T) -> Option<&'node RefCell<T>>, ) -> Option<Result<&mut T, BorrowMutError>>

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.

Examples found in repository?
examples/cyclic.rs (line 47)
23fn main() {
24    let cycle_a = Rc::new(RefCell::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let cycle_b = Rc::new(RefCell::new(CyclicDataStructure {
29        data: 1_u32,
30        next: None,
31    }));
32    let cycle_c = Rc::new(RefCell::new(CyclicDataStructure {
33        data: 2_u32,
34        next: None,
35    }));
36
37    cycle_a.borrow_mut().insert_next(cycle_b.clone());
38    cycle_b.borrow_mut().insert_next(cycle_c.clone());
39    cycle_c.borrow_mut().insert_next(cycle_a.clone());
40
41    // Using a MutRefStack to descend *and then ascend* the data structure.
42    // This cannot be done with regular mutable references.
43    let mut stack = RefCellRefMutStack::new(&cycle_a).expect("not mutable borrowed yet");
44    println!("Stack currently at item with value: {}", stack.top().data);
45    loop {
46        if let Err(_borrow_error) = stack
47            .descend_with(CyclicDataStructure::next)
48            .expect("no node has no next")
49        {
50            println!("Found a cycle!");
51            break;
52        }
53        println!("Descended successfully!");
54        println!("Stack currently at item with value: {}", stack.top().data);
55    }
56    println!("Stack currently at item with value: {}", stack.top().data);
57    loop {
58        if let None = stack.ascend() {
59            println!("Reached the head of the linked list!");
60            break;
61        }
62        println!("Ascended successfully!");
63        println!("Stack currently at item with value: {}", stack.top().data);
64    }
65
66    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
67    stack.top_mut().take_next();
68}
Source

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

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. If we are already at the root, returns None (the top is the root and does not change).

Examples found in repository?
examples/cyclic.rs (line 58)
23fn main() {
24    let cycle_a = Rc::new(RefCell::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let cycle_b = Rc::new(RefCell::new(CyclicDataStructure {
29        data: 1_u32,
30        next: None,
31    }));
32    let cycle_c = Rc::new(RefCell::new(CyclicDataStructure {
33        data: 2_u32,
34        next: None,
35    }));
36
37    cycle_a.borrow_mut().insert_next(cycle_b.clone());
38    cycle_b.borrow_mut().insert_next(cycle_c.clone());
39    cycle_c.borrow_mut().insert_next(cycle_a.clone());
40
41    // Using a MutRefStack to descend *and then ascend* the data structure.
42    // This cannot be done with regular mutable references.
43    let mut stack = RefCellRefMutStack::new(&cycle_a).expect("not mutable borrowed yet");
44    println!("Stack currently at item with value: {}", stack.top().data);
45    loop {
46        if let Err(_borrow_error) = stack
47            .descend_with(CyclicDataStructure::next)
48            .expect("no node has no next")
49        {
50            println!("Found a cycle!");
51            break;
52        }
53        println!("Descended successfully!");
54        println!("Stack currently at item with value: {}", stack.top().data);
55    }
56    println!("Stack currently at item with value: {}", stack.top().data);
57    loop {
58        if let None = stack.ascend() {
59            println!("Reached the head of the linked list!");
60            break;
61        }
62        println!("Ascended successfully!");
63        println!("Stack currently at item with value: {}", stack.top().data);
64    }
65
66    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
67    stack.top_mut().take_next();
68}
Source

pub fn ascend_while<P>(&mut self, predicate: P) -> &mut T
where P: FnMut(&mut T) -> 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<F>(&mut self, f: F) -> Result<&mut T, MoveError>
where F: for<'a> FnOnce(&'a mut T) -> MoveDecision<'root, 'a, T>,

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

Source

pub async fn move_with_async<F>(&mut self, f: F) -> Result<&mut T, MoveError>
where F: for<'a> FnOnce(&'a mut T) -> Pin<Box<dyn Future<Output = MoveDecision<'root, 'a, T>> + 'a>>,

Source

pub fn into_top(self) -> RefMut<'root, T>

Return reference to the top element of this stack, forgetting about the stack entirely. Note that this leaks all RefMuts above the top.

Source

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

Pop all RefMuts off the stack and go back to the root.

Trait Implementations§

Source§

impl<'root, T: ?Sized> Drop for RefCellRefMutStack<'root, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

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

§

impl<'root, T> !RefUnwindSafe for RefCellRefMutStack<'root, T>

§

impl<'root, T> !Send for RefCellRefMutStack<'root, T>

§

impl<'root, T> !Sync for RefCellRefMutStack<'root, T>

§

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

§

impl<'root, T> !UnwindSafe for RefCellRefMutStack<'root, T>

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.