Skip to content

Commit

Permalink
Auto merge of #84650 - a1phyr:simplify_mutex_into_inner, r=m-ou-se
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Apr 28, 2021
2 parents c488f15 + 0b7b121 commit da43ee8
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions library/std/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ mod tests;

use crate::cell::UnsafeCell;
use crate::fmt;
use crate::mem;
use crate::ops::{Deref, DerefMut};
use crate::ptr;
use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
use crate::sys_common::mutex as sys;

Expand Down Expand Up @@ -376,23 +374,8 @@ impl<T: ?Sized> Mutex<T> {
where
T: Sized,
{
// We know statically that there are no outstanding references to
// `self` so there's no need to lock the inner mutex.
//
// 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())
}
let data = self.data.into_inner();
poison::map_result(self.poison.borrow(), |_| data)
}

/// Returns a mutable reference to the underlying data.
Expand Down

0 comments on commit da43ee8

Please sign in to comment.