From ea9408d23e5fdb324d1539f23b48c4e197c0621b Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Thu, 19 Sep 2024 11:03:56 +0000 Subject: [PATCH] Implement Drop for Mutex to drop the contents of the inner MaybeUninit --- src/mutex.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/mutex.rs b/src/mutex.rs index 46e1e8b..d99fbad 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -110,8 +110,10 @@ impl Mutex { /// Unwraps the contained value, consuming the mutex. #[inline] pub fn into_inner(self) -> T { - // Safety: inner is always initialized - unsafe { self.inner.assume_init() } + // Safety: + // - inner is always initialized + // - self will be dropped at the end of the function, _not_ dropping the contents of MaybeUninit + unsafe { self.inner.as_ptr().read() } } /// Borrows the data for the duration of the critical section. @@ -199,6 +201,15 @@ impl Mutex> { } } +impl Drop for Mutex { + fn drop(&mut self) { + // Safety: + // - inner is always initialized + // - self will be dropped at the end of the function, _not_ dropping the contents of MaybeUninit + core::mem::drop(unsafe { self.inner.as_ptr().read() }); + } +} + // NOTE A `Mutex` can be used as a channel so the protected data must be `Send` // to prevent sending non-Sendable stuff (e.g. access tokens) across different // threads.