pub struct MutexGuardStack<'root, T: ?Sized> { /* private fields */ }Implementations§
Source§impl<'root, T: ?Sized> MutexGuardStack<'root, T>
impl<'root, T: ?Sized> MutexGuardStack<'root, T>
Sourcepub fn new(root: &'root Mutex<T>) -> TryLockResult<Self>
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?
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}pub fn raw_top_mut(&mut self) -> *mut T
Sourcepub fn top(&self) -> &T
pub fn top(&self) -> &T
Obtain a shared reference to the top of the stack.
Examples found in repository?
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}Sourcepub fn top_mut(&mut self) -> &mut T
pub fn top_mut(&mut self) -> &mut T
Obtain a mutable reference to the top of the stack.
Examples found in repository?
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}Sourcepub fn is_at_root(&self) -> bool
pub fn is_at_root(&self) -> bool
Is this MutRefStack currently at its root?
Sourcepub fn inject_top(
&mut self,
new_top: &'root Mutex<T>,
ignore_poison: bool,
) -> Result<&mut T, TryLockError<()>>
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.
Sourcepub fn inject_with(
&mut self,
f: impl FnOnce(&mut T) -> Option<&'root Mutex<T>>,
ignore_poison: bool,
) -> Option<Result<&mut T, TryLockError<()>>>
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.
Sourcepub 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<()>>>
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?
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}Sourcepub fn ascend(&mut self) -> Option<&mut T>
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?
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}Sourcepub fn ascend_while<P>(&mut self, predicate: P) -> &mut T
pub fn ascend_while<P>(&mut self, predicate: P) -> &mut T
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<F>(
&mut self,
f: F,
ignore_poison: bool,
) -> Result<&mut T, MoveError>
pub fn move_with<F>( &mut self, f: F, ignore_poison: bool, ) -> Result<&mut T, MoveError>
Ascend from, descend from, inject a new stack top, or stay at the current node, based on the return value of the closure.
pub async fn move_with_async<F>( &mut self, f: F, ignore_poison: bool, ) -> Result<&mut T, MoveError>
Sourcepub fn into_top(self) -> MutexGuard<'root, T>
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.