Skip to content

Commit

Permalink
Replace unneeded unsafe calls to .get() with calls to .get_mut()
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhenrymantilla committed Sep 20, 2020
1 parent 8169989 commit 5886c38
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 16 deletions.
8 changes: 2 additions & 6 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,7 @@ impl<T: ?Sized> Cell<T> {
#[inline]
#[stable(feature = "cell_get_mut", since = "1.11.0")]
pub fn get_mut(&mut self) -> &mut T {
// SAFETY: This can cause data races if called from a separate thread,
// but `Cell` is `!Sync` so this won't happen, and `&mut` guarantees
// unique access.
unsafe { &mut *self.value.get() }
self.value.get_mut()
}

/// Returns a `&Cell<T>` from a `&mut T`
Expand Down Expand Up @@ -945,8 +942,7 @@ impl<T: ?Sized> RefCell<T> {
#[inline]
#[stable(feature = "cell_get_mut", since = "1.11.0")]
pub fn get_mut(&mut self) -> &mut T {
// SAFETY: `&mut` guarantees unique access.
unsafe { &mut *self.value.get() }
self.value.get_mut()
}

/// Undo the effect of leaked guards on the borrow state of the `RefCell`.
Expand Down
6 changes: 2 additions & 4 deletions library/core/src/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,7 @@ impl<T> AtomicPtr<T> {
#[inline]
#[stable(feature = "atomic_access", since = "1.15.0")]
pub fn get_mut(&mut self) -> &mut *mut T {
// SAFETY: the mutable reference guarantees unique ownership.
unsafe { &mut *self.p.get() }
self.p.get_mut()
}

/// Get atomic access to a pointer.
Expand Down Expand Up @@ -1275,8 +1274,7 @@ assert_eq!(some_var.load(Ordering::SeqCst), 5);
#[inline]
#[$stable_access]
pub fn get_mut(&mut self) -> &mut $int_type {
// SAFETY: the mutable reference guarantees unique ownership.
unsafe { &mut *self.v.get() }
self.v.get_mut()
}
}

Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@
#![feature(try_reserve)]
#![feature(unboxed_closures)]
#![feature(unsafe_block_in_unsafe_fn)]
#![feature(unsafe_cell_get_mut)]
#![feature(unsafe_cell_raw_get)]
#![feature(untagged_unions)]
#![feature(unwind_attributes)]
Expand Down
4 changes: 1 addition & 3 deletions library/std/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,7 @@ impl<T: ?Sized> Mutex<T> {
/// ```
#[stable(feature = "mutex_get_mut", since = "1.6.0")]
pub fn get_mut(&mut self) -> LockResult<&mut T> {
// We know statically that there are no other references to `self`, so
// there's no need to lock the inner mutex.
let data = unsafe { &mut *self.data.get() };
let data = self.data.get_mut();
poison::map_result(self.poison.borrow(), |_| data)
}
}
Expand Down
4 changes: 1 addition & 3 deletions library/std/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,7 @@ impl<T: ?Sized> RwLock<T> {
/// ```
#[stable(feature = "rwlock_get_mut", since = "1.6.0")]
pub fn get_mut(&mut self) -> LockResult<&mut T> {
// We know statically that there are no other references to `self`, so
// there's no need to lock the inner lock.
let data = unsafe { &mut *self.data.get() };
let data = self.data.get_mut();
poison::map_result(self.poison.borrow(), |_| data)
}
}
Expand Down

0 comments on commit 5886c38

Please sign in to comment.