Skip to content

Commit fa1656e

Browse files
committed
Add safety comments
1 parent 0157593 commit fa1656e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

library/std/src/sync/rwlock.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,8 @@ impl<T> From<T> for RwLock<T> {
512512

513513
impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
514514
/// 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.
515+
// SAFETY: if and only if `lock.inner.read()` (or `lock.inner.try_read()`) has been
516+
// successfully called from the same thread before instantiating this object.
518517
unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> {
519518
poison::map_result(lock.poison.borrow(), |()| RwLockReadGuard {
520519
data: NonNull::new_unchecked(lock.data.get()),
@@ -525,9 +524,8 @@ impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
525524

526525
impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
527526
/// Create a new instance of `RwLockWriteGuard<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.
527+
// SAFETY: if and only if `lock.inner.write()` (or `lock.inner.try_write()`) has been
528+
// successfully called from the same thread before instantiating this object.
531529
unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockWriteGuard<'rwlock, T>> {
532530
poison::map_result(lock.poison.guard(), |guard| RwLockWriteGuard { lock, poison: guard })
533531
}
@@ -566,6 +564,7 @@ impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
566564
type Target = T;
567565

568566
fn deref(&self) -> &T {
567+
// SAFETY: the conditions of `RwLockGuard::new` were satisfied when created.
569568
unsafe { self.data.as_ref() }
570569
}
571570
}
@@ -575,20 +574,23 @@ impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
575574
type Target = T;
576575

577576
fn deref(&self) -> &T {
577+
// SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
578578
unsafe { &*self.lock.data.get() }
579579
}
580580
}
581581

582582
#[stable(feature = "rust1", since = "1.0.0")]
583583
impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
584584
fn deref_mut(&mut self) -> &mut T {
585+
// SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
585586
unsafe { &mut *self.lock.data.get() }
586587
}
587588
}
588589

589590
#[stable(feature = "rust1", since = "1.0.0")]
590591
impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
591592
fn drop(&mut self) {
593+
// SAFETY: the conditions of `RwLockReadGuard::new` were satisfied when created.
592594
unsafe {
593595
self.inner_lock.read_unlock();
594596
}
@@ -599,6 +601,7 @@ impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
599601
impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
600602
fn drop(&mut self) {
601603
self.lock.poison.done(&self.poison);
604+
// SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
602605
unsafe {
603606
self.lock.inner.write_unlock();
604607
}

0 commit comments

Comments
 (0)