Skip to content

Commit 5491dfe

Browse files
Add Mutex::{get_mut, into_inner}
1 parent 607b583 commit 5491dfe

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,20 @@ impl<T> Mutex<T> {
5555
inner: UnsafeCell::new(value),
5656
}
5757
}
58-
}
5958

60-
impl<T> Mutex<T> {
59+
/// Gets a mutable reference to the contained value when the mutex is already uniquely borrowed.
60+
///
61+
/// This does not require locking or a critical section since it takes `&mut self`, which
62+
/// guarantees unique ownership already.
63+
pub fn get_mut(&mut self) -> &mut T {
64+
unsafe { &mut *self.inner.get() }
65+
}
66+
67+
/// Unwraps the contained value, consuming the mutex.
68+
pub fn into_inner(self) -> T {
69+
self.inner.into_inner()
70+
}
71+
6172
/// Borrows the data for the duration of the critical section.
6273
pub fn borrow<'cs>(&'cs self, _cs: CriticalSection<'cs>) -> &'cs T {
6374
unsafe { &*self.inner.get() }

0 commit comments

Comments
 (0)