Auto merge of #84650 - a1phyr:simplify_mutex_into_inner, r=m-ou-se

Simplify `Mutex::into_inner`

Thanks to #77147, `Mutex` do not implement `Drop` directly, so the old unsafe implementation of `into_inner` is not relevant anymore.
This commit is contained in:
bors 2021-04-28 18:08:01 +00:00
commit da43ee8d82

View File

@ -3,9 +3,7 @@
use crate::cell::UnsafeCell; use crate::cell::UnsafeCell;
use crate::fmt; use crate::fmt;
use crate::mem;
use crate::ops::{Deref, DerefMut}; use crate::ops::{Deref, DerefMut};
use crate::ptr;
use crate::sync::{poison, LockResult, TryLockError, TryLockResult}; use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
use crate::sys_common::mutex as sys; use crate::sys_common::mutex as sys;
@ -376,23 +374,8 @@ pub fn into_inner(self) -> LockResult<T>
where where
T: Sized, T: Sized,
{ {
// We know statically that there are no outstanding references to let data = self.data.into_inner();
// `self` so there's no need to lock the inner mutex. poison::map_result(self.poison.borrow(), |_| data)
//
// To get the inner value, we'd like to call `data.into_inner()`,
// but because `Mutex` impl-s `Drop`, we can't move out of it, so
// we'll have to destructure it manually instead.
unsafe {
// Like `let Mutex { inner, poison, data } = self`.
let (inner, poison, data) = {
let Mutex { ref inner, ref poison, ref data } = self;
(ptr::read(inner), ptr::read(poison), ptr::read(data))
};
mem::forget(self);
drop(inner);
poison::map_result(poison.borrow(), |_| data.into_inner())
}
} }
/// Returns a mutable reference to the underlying data. /// Returns a mutable reference to the underlying data.