diff options
| author | Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com> | 2020-09-19 21:33:40 +0200 |
|---|---|---|
| committer | Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com> | 2020-09-20 18:06:03 +0200 |
| commit | 5886c38112c8bb347b1cbd46c28b1ca6f8bac88d (patch) | |
| tree | 04dfeb868a19be6a6d953c8756242d0530a99bfe | |
| parent | 81699895073162cd6731413711a4357dde67d661 (diff) | |
| download | rust-5886c38112c8bb347b1cbd46c28b1ca6f8bac88d.tar.gz rust-5886c38112c8bb347b1cbd46c28b1ca6f8bac88d.zip | |
Replace unneeded `unsafe` calls to `.get()` with calls to `.get_mut()`
| -rw-r--r-- | library/core/src/cell.rs | 8 | ||||
| -rw-r--r-- | library/core/src/sync/atomic.rs | 6 | ||||
| -rw-r--r-- | library/std/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/std/src/sync/mutex.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sync/rwlock.rs | 4 |
5 files changed, 7 insertions, 16 deletions
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index 44b863b2200..f60aa2d24c5 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -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` @@ -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`. diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs index 9d74f537491..c67d6422c01 100644 --- a/library/core/src/sync/atomic.rs +++ b/library/core/src/sync/atomic.rs @@ -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. @@ -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() } } diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 5333d75ec1b..71b29cf5af9 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -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)] diff --git a/library/std/src/sync/mutex.rs b/library/std/src/sync/mutex.rs index 240155b06b4..a1703c731d4 100644 --- a/library/std/src/sync/mutex.rs +++ b/library/std/src/sync/mutex.rs @@ -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) } } diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs index f38d6101da0..d967779ce36 100644 --- a/library/std/src/sync/rwlock.rs +++ b/library/std/src/sync/rwlock.rs @@ -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) } } |
