MutexGuardStack

Struct MutexGuardStack 

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

Implementations§

Source§

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

Source

pub fn new(root: &'root Mutex<T>) -> TryLockResult<Self>

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

Examples found in repository?
examples/cyclic_sync.rs (line 39)
23fn main() {
24    let cycle_root = Arc::new(Mutex::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let mut current = cycle_root.clone();
29    for i in (1..128).rev() {
30        current = Arc::new(Mutex::new(CyclicDataStructure {
31            data: i,
32            next: Some(current),
33        }))
34    }
35    cycle_root.lock().unwrap().next = Some(current);
36
37    // Using a MutRefStack to descend *and then ascend* the data structure.
38    // This cannot be done with regular mutable references.
39    let mut stack = MutexGuardStack::new(&cycle_root).expect("not mutable borrowed yet");
40    println!("Stack currently at item with value: {}", stack.top().data);
41    loop {
42        if let Err(_borrow_error) = stack
43            .descend_with(CyclicDataStructure::next, false)
44            .expect("no node has no next")
45        {
46            println!("Found a cycle!");
47            break;
48        }
49        println!("Descended successfully!");
50        println!("Stack currently at item with value: {}", stack.top().data);
51    }
52    println!("Stack currently at item with value: {}", stack.top().data);
53    loop {
54        if let None = stack.ascend() {
55            println!("Reached the head of the linked list!");
56            break;
57        }
58        println!("Ascended successfully!");
59        println!("Stack currently at item with value: {}", stack.top().data);
60    }
61
62    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
63    stack.top_mut().take_next();
64}
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_sync.rs (line 40)
23fn main() {
24    let cycle_root = Arc::new(Mutex::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let mut current = cycle_root.clone();
29    for i in (1..128).rev() {
30        current = Arc::new(Mutex::new(CyclicDataStructure {
31            data: i,
32            next: Some(current),
33        }))
34    }
35    cycle_root.lock().unwrap().next = Some(current);
36
37    // Using a MutRefStack to descend *and then ascend* the data structure.
38    // This cannot be done with regular mutable references.
39    let mut stack = MutexGuardStack::new(&cycle_root).expect("not mutable borrowed yet");
40    println!("Stack currently at item with value: {}", stack.top().data);
41    loop {
42        if let Err(_borrow_error) = stack
43            .descend_with(CyclicDataStructure::next, false)
44            .expect("no node has no next")
45        {
46            println!("Found a cycle!");
47            break;
48        }
49        println!("Descended successfully!");
50        println!("Stack currently at item with value: {}", stack.top().data);
51    }
52    println!("Stack currently at item with value: {}", stack.top().data);
53    loop {
54        if let None = stack.ascend() {
55            println!("Reached the head of the linked list!");
56            break;
57        }
58        println!("Ascended successfully!");
59        println!("Stack currently at item with value: {}", stack.top().data);
60    }
61
62    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
63    stack.top_mut().take_next();
64}
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_sync.rs (line 63)
23fn main() {
24    let cycle_root = Arc::new(Mutex::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let mut current = cycle_root.clone();
29    for i in (1..128).rev() {
30        current = Arc::new(Mutex::new(CyclicDataStructure {
31            data: i,
32            next: Some(current),
33        }))
34    }
35    cycle_root.lock().unwrap().next = Some(current);
36
37    // Using a MutRefStack to descend *and then ascend* the data structure.
38    // This cannot be done with regular mutable references.
39    let mut stack = MutexGuardStack::new(&cycle_root).expect("not mutable borrowed yet");
40    println!("Stack currently at item with value: {}", stack.top().data);
41    loop {
42        if let Err(_borrow_error) = stack
43            .descend_with(CyclicDataStructure::next, false)
44            .expect("no node has no next")
45        {
46            println!("Found a cycle!");
47            break;
48        }
49        println!("Descended successfully!");
50        println!("Stack currently at item with value: {}", stack.top().data);
51    }
52    println!("Stack currently at item with value: {}", stack.top().data);
53    loop {
54        if let None = stack.ascend() {
55            println!("Reached the head of the linked list!");
56            break;
57        }
58        println!("Ascended successfully!");
59        println!("Stack currently at item with value: {}", stack.top().data);
60    }
61
62    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
63    stack.top_mut().take_next();
64}
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 Mutex<T>, ignore_poison: bool, ) -> Result<&mut T, TryLockError<()>>

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 Mutex<T>>, ignore_poison: bool, ) -> Option<Result<&mut T, TryLockError<()>>>

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 Mutex<T>>, ignore_poison: bool, ) -> Option<Result<&mut T, TryLockError<()>>>

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_sync.rs (line 43)
23fn main() {
24    let cycle_root = Arc::new(Mutex::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let mut current = cycle_root.clone();
29    for i in (1..128).rev() {
30        current = Arc::new(Mutex::new(CyclicDataStructure {
31            data: i,
32            next: Some(current),
33        }))
34    }
35    cycle_root.lock().unwrap().next = Some(current);
36
37    // Using a MutRefStack to descend *and then ascend* the data structure.
38    // This cannot be done with regular mutable references.
39    let mut stack = MutexGuardStack::new(&cycle_root).expect("not mutable borrowed yet");
40    println!("Stack currently at item with value: {}", stack.top().data);
41    loop {
42        if let Err(_borrow_error) = stack
43            .descend_with(CyclicDataStructure::next, false)
44            .expect("no node has no next")
45        {
46            println!("Found a cycle!");
47            break;
48        }
49        println!("Descended successfully!");
50        println!("Stack currently at item with value: {}", stack.top().data);
51    }
52    println!("Stack currently at item with value: {}", stack.top().data);
53    loop {
54        if let None = stack.ascend() {
55            println!("Reached the head of the linked list!");
56            break;
57        }
58        println!("Ascended successfully!");
59        println!("Stack currently at item with value: {}", stack.top().data);
60    }
61
62    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
63    stack.top_mut().take_next();
64}
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_sync.rs (line 54)
23fn main() {
24    let cycle_root = Arc::new(Mutex::new(CyclicDataStructure {
25        data: 0_u32,
26        next: None,
27    }));
28    let mut current = cycle_root.clone();
29    for i in (1..128).rev() {
30        current = Arc::new(Mutex::new(CyclicDataStructure {
31            data: i,
32            next: Some(current),
33        }))
34    }
35    cycle_root.lock().unwrap().next = Some(current);
36
37    // Using a MutRefStack to descend *and then ascend* the data structure.
38    // This cannot be done with regular mutable references.
39    let mut stack = MutexGuardStack::new(&cycle_root).expect("not mutable borrowed yet");
40    println!("Stack currently at item with value: {}", stack.top().data);
41    loop {
42        if let Err(_borrow_error) = stack
43            .descend_with(CyclicDataStructure::next, false)
44            .expect("no node has no next")
45        {
46            println!("Found a cycle!");
47            break;
48        }
49        println!("Descended successfully!");
50        println!("Stack currently at item with value: {}", stack.top().data);
51    }
52    println!("Stack currently at item with value: {}", stack.top().data);
53    loop {
54        if let None = stack.ascend() {
55            println!("Reached the head of the linked list!");
56            break;
57        }
58        println!("Ascended successfully!");
59        println!("Stack currently at item with value: {}", stack.top().data);
60    }
61
62    println!("(Breaking the cycle to prevent miri from complaining about memory leaks)");
63    stack.top_mut().take_next();
64}
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, ignore_poison: bool, ) -> 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, ignore_poison: bool, ) -> 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) -> MutexGuard<'root, T>

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

Source

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

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

Trait Implementations§

Source§

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

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

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

§

impl<'root, T> RefUnwindSafe for MutexGuardStack<'root, T>
where T: RefUnwindSafe + ?Sized,

§

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

§

impl<'root, T> Sync for MutexGuardStack<'root, T>
where T: Sync + ?Sized,

§

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

§

impl<'root, T> !UnwindSafe for MutexGuardStack<'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.