@@ -4,7 +4,7 @@ mod tests;
4
4
use crate :: cell:: UnsafeCell ;
5
5
use crate :: fmt;
6
6
use crate :: marker:: PhantomData ;
7
- use crate :: mem:: ManuallyDrop ;
7
+ use crate :: mem:: { forget , ManuallyDrop } ;
8
8
use crate :: ops:: { Deref , DerefMut } ;
9
9
use crate :: ptr:: NonNull ;
10
10
use crate :: sync:: { poison, LockResult , TryLockError , TryLockResult } ;
@@ -969,13 +969,18 @@ impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
969
969
/// MORE DOCS COMING SOON.
970
970
#[ unstable( feature = "rwlock_downgrade" , issue = "128203" ) ]
971
971
pub fn downgrade ( s : Self ) -> RwLockReadGuard < ' a , T > {
972
+ let lock = s. lock ;
973
+
972
974
// SAFETY: We take ownership of a write guard, so we must already have the `RwLock` in write
973
975
// mode, satisfying the `downgrade` contract.
974
- unsafe { s. lock . inner . downgrade ( ) } ;
976
+ unsafe { lock. inner . downgrade ( ) } ;
977
+
978
+ // We don't want to call the destructor since that calls `write_unlock`.
979
+ forget ( s) ;
975
980
976
981
// SAFETY: We have just successfully called `downgrade`, so we fulfill the safety contract.
977
982
unsafe {
978
- RwLockReadGuard :: new ( s . lock )
983
+ RwLockReadGuard :: new ( lock)
979
984
. expect ( "We had the exclusive lock so we can't have panicked while holding it" )
980
985
}
981
986
}
0 commit comments