@@ -512,9 +512,8 @@ impl<T> From<T> for RwLock<T> {
512
512
513
513
impl < ' rwlock , T : ?Sized > RwLockReadGuard < ' rwlock , T > {
514
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.
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.
518
517
unsafe fn new ( lock : & ' rwlock RwLock < T > ) -> LockResult < RwLockReadGuard < ' rwlock , T > > {
519
518
poison:: map_result ( lock. poison . borrow ( ) , |( ) | RwLockReadGuard {
520
519
data : NonNull :: new_unchecked ( lock. data . get ( ) ) ,
@@ -525,9 +524,8 @@ impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
525
524
526
525
impl < ' rwlock , T : ?Sized > RwLockWriteGuard < ' rwlock , T > {
527
526
/// 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.
531
529
unsafe fn new ( lock : & ' rwlock RwLock < T > ) -> LockResult < RwLockWriteGuard < ' rwlock , T > > {
532
530
poison:: map_result ( lock. poison . guard ( ) , |guard| RwLockWriteGuard { lock, poison : guard } )
533
531
}
@@ -566,6 +564,7 @@ impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
566
564
type Target = T ;
567
565
568
566
fn deref ( & self ) -> & T {
567
+ // SAFETY: the conditions of `RwLockGuard::new` were satisfied when created.
569
568
unsafe { self . data . as_ref ( ) }
570
569
}
571
570
}
@@ -575,20 +574,23 @@ impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
575
574
type Target = T ;
576
575
577
576
fn deref ( & self ) -> & T {
577
+ // SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
578
578
unsafe { & * self . lock . data . get ( ) }
579
579
}
580
580
}
581
581
582
582
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
583
583
impl < T : ?Sized > DerefMut for RwLockWriteGuard < ' _ , T > {
584
584
fn deref_mut ( & mut self ) -> & mut T {
585
+ // SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
585
586
unsafe { & mut * self . lock . data . get ( ) }
586
587
}
587
588
}
588
589
589
590
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
590
591
impl < T : ?Sized > Drop for RwLockReadGuard < ' _ , T > {
591
592
fn drop ( & mut self ) {
593
+ // SAFETY: the conditions of `RwLockReadGuard::new` were satisfied when created.
592
594
unsafe {
593
595
self . inner_lock . read_unlock ( ) ;
594
596
}
@@ -599,6 +601,7 @@ impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
599
601
impl < T : ?Sized > Drop for RwLockWriteGuard < ' _ , T > {
600
602
fn drop ( & mut self ) {
601
603
self . lock . poison . done ( & self . poison ) ;
604
+ // SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
602
605
unsafe {
603
606
self . lock . inner . write_unlock ( ) ;
604
607
}
0 commit comments