From 90e789672f1e3140e96413e4815b354190a8d7e4 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Sun, 28 Aug 2022 08:06:52 +0800 Subject: [PATCH] Add `ArcMutexGuard::into_arc` This allows the contained `Arc` to be recovered after unlocking the mutex. --- lock_api/src/mutex.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lock_api/src/mutex.rs b/lock_api/src/mutex.rs index 4e1b879e..4982503b 100644 --- a/lock_api/src/mutex.rs +++ b/lock_api/src/mutex.rs @@ -692,6 +692,18 @@ impl ArcMutexGuard { &self.mutex } + /// Unlocks the mutex and returns the `Arc` that was held by the [`ArcMutexGuard`]. + #[inline] + pub fn into_arc(self) -> Arc> { + // 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