@@ -5,6 +5,7 @@ use crate::cell::UnsafeCell;
55use crate :: fmt;
66use crate :: mem;
77use crate :: ops:: { Deref , DerefMut } ;
8+ use crate :: pin:: Pin ;
89use crate :: ptr;
910use crate :: sys_common:: poison:: { self , LockResult , TryLockError , TryLockResult } ;
1011use crate :: sys_common:: rwlock as sys;
@@ -64,7 +65,7 @@ use crate::sys_common::rwlock as sys;
6465/// [`Mutex`]: super::Mutex
6566#[ stable( feature = "rust1" , since = "1.0.0" ) ]
6667pub struct RwLock < T : ?Sized > {
67- inner : Box < sys:: RWLock > ,
68+ inner : Pin < Box < sys:: RWLock > > ,
6869 poison : poison:: Flag ,
6970 data : UnsafeCell < T > ,
7071}
@@ -128,7 +129,7 @@ impl<T> RwLock<T> {
128129 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
129130 pub fn new ( t : T ) -> RwLock < T > {
130131 RwLock {
131- inner : box sys:: RWLock :: new ( ) ,
132+ inner : Box :: pin ( sys:: RWLock :: new ( ) ) ,
132133 poison : poison:: Flag :: new ( ) ,
133134 data : UnsafeCell :: new ( t) ,
134135 }
@@ -178,10 +179,9 @@ impl<T: ?Sized> RwLock<T> {
178179 #[ inline]
179180 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
180181 pub fn read ( & self ) -> LockResult < RwLockReadGuard < ' _ , T > > {
181- unsafe {
182- self . inner . read ( ) ;
183- RwLockReadGuard :: new ( self )
184- }
182+ self . inner . as_ref ( ) . read ( ) ;
183+ // SAFETY: We've gotten a read-lock on the rwlock.
184+ unsafe { RwLockReadGuard :: new ( self ) }
185185 }
186186
187187 /// Attempts to acquire this rwlock with shared read access.
@@ -217,12 +217,11 @@ impl<T: ?Sized> RwLock<T> {
217217 #[ inline]
218218 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
219219 pub fn try_read ( & self ) -> TryLockResult < RwLockReadGuard < ' _ , T > > {
220- unsafe {
221- if self . inner . try_read ( ) {
222- Ok ( RwLockReadGuard :: new ( self ) ?)
223- } else {
224- Err ( TryLockError :: WouldBlock )
225- }
220+ if self . inner . as_ref ( ) . try_read ( ) {
221+ // SAFETY: We've gotten a read-lock on the rwlock.
222+ unsafe { Ok ( RwLockReadGuard :: new ( self ) ?) }
223+ } else {
224+ Err ( TryLockError :: WouldBlock )
226225 }
227226 }
228227
@@ -260,10 +259,9 @@ impl<T: ?Sized> RwLock<T> {
260259 #[ inline]
261260 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
262261 pub fn write ( & self ) -> LockResult < RwLockWriteGuard < ' _ , T > > {
263- unsafe {
264- self . inner . write ( ) ;
265- RwLockWriteGuard :: new ( self )
266- }
262+ self . inner . as_ref ( ) . write ( ) ;
263+ // SAFETY: We've gotten a write-lock on the rwlock.
264+ unsafe { RwLockWriteGuard :: new ( self ) }
267265 }
268266
269267 /// Attempts to lock this rwlock with exclusive write access.
@@ -299,12 +297,11 @@ impl<T: ?Sized> RwLock<T> {
299297 #[ inline]
300298 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
301299 pub fn try_write ( & self ) -> TryLockResult < RwLockWriteGuard < ' _ , T > > {
302- unsafe {
303- if self . inner . try_write ( ) {
304- Ok ( RwLockWriteGuard :: new ( self ) ?)
305- } else {
306- Err ( TryLockError :: WouldBlock )
307- }
300+ if self . inner . as_ref ( ) . try_write ( ) {
301+ // SAFETY: We've gotten a write-lock on the rwlock.
302+ unsafe { Ok ( RwLockWriteGuard :: new ( self ) ?) }
303+ } else {
304+ Err ( TryLockError :: WouldBlock )
308305 }
309306 }
310307
@@ -374,7 +371,6 @@ impl<T: ?Sized> RwLock<T> {
374371 ( ptr:: read ( inner) , ptr:: read ( poison) , ptr:: read ( data) )
375372 } ;
376373 mem:: forget ( self ) ;
377- inner. destroy ( ) ; // Keep in sync with the `Drop` impl.
378374 drop ( inner) ;
379375
380376 poison:: map_result ( poison. borrow ( ) , |_| data. into_inner ( ) )
@@ -409,14 +405,6 @@ impl<T: ?Sized> RwLock<T> {
409405 }
410406}
411407
412- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
413- unsafe impl < #[ may_dangle] T : ?Sized > Drop for RwLock < T > {
414- fn drop ( & mut self ) {
415- // IMPORTANT: This code needs to be kept in sync with `RwLock::into_inner`.
416- unsafe { self . inner . destroy ( ) }
417- }
418- }
419-
420408#[ stable( feature = "rust1" , since = "1.0.0" ) ]
421409impl < T : ?Sized + fmt:: Debug > fmt:: Debug for RwLock < T > {
422410 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -524,8 +512,10 @@ impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
524512#[ stable( feature = "rust1" , since = "1.0.0" ) ]
525513impl < T : ?Sized > Drop for RwLockReadGuard < ' _ , T > {
526514 fn drop ( & mut self ) {
515+ // SAFETY: The fact that we have a RwLockReadGuard proves this thread
516+ // has this rwlock read-locked.
527517 unsafe {
528- self . lock . inner . read_unlock ( ) ;
518+ self . lock . inner . as_ref ( ) . read_unlock ( ) ;
529519 }
530520 }
531521}
@@ -534,8 +524,10 @@ impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
534524impl < T : ?Sized > Drop for RwLockWriteGuard < ' _ , T > {
535525 fn drop ( & mut self ) {
536526 self . lock . poison . done ( & self . poison ) ;
527+ // SAFETY: The fact that we have a RwLockWriteGuard proves this thread
528+ // has this rwlock write-locked.
537529 unsafe {
538- self . lock . inner . write_unlock ( ) ;
530+ self . lock . inner . as_ref ( ) . write_unlock ( ) ;
539531 }
540532 }
541533}
0 commit comments