From 86b781588691d8fe4c75618e6ac3d946ddccd423 Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Wed, 23 Mar 2022 21:25:39 +0100 Subject: [PATCH] add `MutexGuard::leak` --- lock_api/src/mutex.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lock_api/src/mutex.rs b/lock_api/src/mutex.rs index ad8ea3c5..4e1b879e 100644 --- a/lock_api/src/mutex.rs +++ b/lock_api/src/mutex.rs @@ -565,6 +565,17 @@ impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> MutexGuard<'a, R, T> { defer!(s.mutex.raw.lock()); f() } + + /// Leaks the mutex guard and returns a mutable reference to the data + /// protected by the mutex. + /// + /// This will leave the `Mutex` in a locked state. + #[inline] + pub fn leak(s: Self) -> &'a mut T { + let r = unsafe { &mut *s.mutex.data.get() }; + mem::forget(s); + r + } } impl<'a, R: RawMutexFair + 'a, T: ?Sized + 'a> MutexGuard<'a, R, T> {