Skip to content

Commit ea9408d

Browse files
committed
Implement Drop for Mutex to drop the contents of the inner MaybeUninit
1 parent f0cd528 commit ea9408d

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/mutex.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ impl<T> Mutex<T> {
110110
/// Unwraps the contained value, consuming the mutex.
111111
#[inline]
112112
pub fn into_inner(self) -> T {
113-
// Safety: inner is always initialized
114-
unsafe { self.inner.assume_init() }
113+
// Safety:
114+
// - inner is always initialized
115+
// - self will be dropped at the end of the function, _not_ dropping the contents of MaybeUninit
116+
unsafe { self.inner.as_ptr().read() }
115117
}
116118

117119
/// Borrows the data for the duration of the critical section.
@@ -199,6 +201,15 @@ impl<T: Default> Mutex<RefCell<T>> {
199201
}
200202
}
201203

204+
impl<T> Drop for Mutex<T> {
205+
fn drop(&mut self) {
206+
// Safety:
207+
// - inner is always initialized
208+
// - self will be dropped at the end of the function, _not_ dropping the contents of MaybeUninit
209+
core::mem::drop(unsafe { self.inner.as_ptr().read() });
210+
}
211+
}
212+
202213
// NOTE A `Mutex` can be used as a channel so the protected data must be `Send`
203214
// to prevent sending non-Sendable stuff (e.g. access tokens) across different
204215
// threads.

0 commit comments

Comments
 (0)