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.