Skip to content

Commit

Permalink
Add get_mut() to Mutex and RwLock (#322)
Browse files Browse the repository at this point in the history
Add the standard library get_mut() method to Mutex and RwLock. This
provides mutable access without locking for callers who already hold a
&mut Mutex or &mut RwLock.
  • Loading branch information
akonradi authored Aug 9, 2023
1 parent 053377c commit ddf871f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ impl<T> Mutex<T> {
}
}

/// Returns a mutable reference to the underlying data.
pub fn get_mut(&mut self) -> LockResult<&mut T> {
Ok(self.data.get_mut().unwrap())
}

/// Consumes this mutex, returning the underlying data.
pub fn into_inner(self) -> LockResult<T> {
Ok(self.data.into_inner().unwrap())
Expand Down
5 changes: 5 additions & 0 deletions src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ impl<T> RwLock<T> {
}
}

/// Returns a mutable reference to the underlying data.
pub fn get_mut(&mut self) -> LockResult<&mut T> {
Ok(self.data.get_mut().expect("loom::RwLock state corrupt"))
}

/// Consumes this `RwLock`, returning the underlying data.
pub fn into_inner(self) -> LockResult<T> {
Ok(self.data.into_inner().expect("loom::RwLock state corrupt"))
Expand Down

0 comments on commit ddf871f

Please sign in to comment.