about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorRobin Raymond <robin@robinraymond.de>2022-05-14 07:29:02 +0000
committerRobin Raymond <robin@robinraymond.de>2022-06-19 09:23:32 +0200
commit08650fbb506c44fbc95d91abfb7cd95a56bbff30 (patch)
tree067367a4e33160f37118d77a6dc83ad16e170c43 /library/std/src
parentcf1238e799b18f38a9c328b6700434e38f98cec7 (diff)
downloadrust-08650fbb506c44fbc95d91abfb7cd95a56bbff30.tar.gz
rust-08650fbb506c44fbc95d91abfb7cd95a56bbff30.zip
*const to NonNull plus documentation
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/sync/rwlock.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs
index ac3c3b509cd..81693a7f5f5 100644
--- a/library/std/src/sync/rwlock.rs
+++ b/library/std/src/sync/rwlock.rs
@@ -4,6 +4,7 @@ mod tests;
 use crate::cell::UnsafeCell;
 use crate::fmt;
 use crate::ops::{Deref, DerefMut};
+use crate::ptr::NonNull;
 use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
 use crate::sys_common::rwlock as sys;
 
@@ -101,7 +102,7 @@ unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
 #[stable(feature = "rust1", since = "1.0.0")]
 #[clippy::has_significant_drop]
 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
-    data: *const T,
+    data: NonNull<T>,
     inner_lock: &'a sys::MovableRwLock,
 }
 
@@ -510,15 +511,23 @@ impl<T> From<T> for RwLock<T> {
 }
 
 impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
+    /// Create a new instance of `RwLockReadGuard<T>` from a `RwLock<T>`.
+    ///
+    /// It is safe to call this function if and only if `lock.inner.read()` (or
+    /// `lock.inner.try_read()`) has been successfully called before instantiating this object.
     unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> {
         poison::map_result(lock.poison.borrow(), |()| RwLockReadGuard {
+            data: NonNull::new_unchecked(lock.data.get()),
             inner_lock: &lock.inner,
-            data: lock.data.get(),
         })
     }
 }
 
 impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
+    /// Create a new instance of `RwLockReadGuard<T>` from a `RwLock<T>`.
+    ///
+    /// It is safe to call this function if and only if `lock.inner.write()` (or
+    /// `lock.inner.try_write()`) has been successfully called before instantiating this object.
     unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockWriteGuard<'rwlock, T>> {
         poison::map_result(lock.poison.guard(), |guard| RwLockWriteGuard { lock, poison: guard })
     }
@@ -557,7 +566,7 @@ impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
     type Target = T;
 
     fn deref(&self) -> &T {
-        unsafe { &*self.data }
+        unsafe { self.data.as_ref() }
     }
 }