Skip to content

Commit

Permalink
Add ArcMutexGuard::into_arc
Browse files Browse the repository at this point in the history
This allows the contained `Arc` to be recovered after unlocking the
mutex.
  • Loading branch information
Amanieu committed Aug 28, 2022
1 parent 336a9b3 commit 90e7896
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lock_api/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,18 @@ impl<R: RawMutex, T: ?Sized> ArcMutexGuard<R, T> {
&self.mutex
}

/// Unlocks the mutex and returns the `Arc` that was held by the [`ArcMutexGuard`].
#[inline]
pub fn into_arc(self) -> Arc<Mutex<R, T>> {
// Safety: Skip our Drop impl and manually unlock the mutex.
let arc = unsafe { ptr::read(&self.mutex) };
mem::forget(self);
unsafe {
arc.raw.unlock();
}
arc
}

/// Temporarily unlocks the mutex to execute the given function.
///
/// This is safe because `&mut` guarantees that there exist no other
Expand Down

0 comments on commit 90e7896

Please sign in to comment.