Skip to content

Commit 08650fb

Browse files
committed
*const to NonNull plus documentation
1 parent cf1238e commit 08650fb

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

library/std/src/sync/rwlock.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod tests;
44
use crate::cell::UnsafeCell;
55
use crate::fmt;
66
use crate::ops::{Deref, DerefMut};
7+
use crate::ptr::NonNull;
78
use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
89
use crate::sys_common::rwlock as sys;
910

@@ -101,7 +102,7 @@ unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
101102
#[stable(feature = "rust1", since = "1.0.0")]
102103
#[clippy::has_significant_drop]
103104
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
104-
data: *const T,
105+
data: NonNull<T>,
105106
inner_lock: &'a sys::MovableRwLock,
106107
}
107108

@@ -510,15 +511,23 @@ impl<T> From<T> for RwLock<T> {
510511
}
511512

512513
impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
514+
/// Create a new instance of `RwLockReadGuard<T>` from a `RwLock<T>`.
515+
///
516+
/// It is safe to call this function if and only if `lock.inner.read()` (or
517+
/// `lock.inner.try_read()`) has been successfully called before instantiating this object.
513518
unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> {
514519
poison::map_result(lock.poison.borrow(), |()| RwLockReadGuard {
520+
data: NonNull::new_unchecked(lock.data.get()),
515521
inner_lock: &lock.inner,
516-
data: lock.data.get(),
517522
})
518523
}
519524
}
520525

521526
impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
527+
/// Create a new instance of `RwLockReadGuard<T>` from a `RwLock<T>`.
528+
///
529+
/// It is safe to call this function if and only if `lock.inner.write()` (or
530+
/// `lock.inner.try_write()`) has been successfully called before instantiating this object.
522531
unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockWriteGuard<'rwlock, T>> {
523532
poison::map_result(lock.poison.guard(), |guard| RwLockWriteGuard { lock, poison: guard })
524533
}
@@ -557,7 +566,7 @@ impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
557566
type Target = T;
558567

559568
fn deref(&self) -> &T {
560-
unsafe { &*self.data }
569+
unsafe { self.data.as_ref() }
561570
}
562571
}
563572

0 commit comments

Comments
 (0)