@@ -3,10 +3,12 @@ use crate::fmt;
33use crate :: marker:: PhantomData ;
44use crate :: mem:: { self , ManuallyDrop , forget} ;
55use crate :: ops:: { Deref , DerefMut } ;
6- use crate :: ptr:: NonNull ;
6+ use crate :: ptr:: { NonNull } ;
77use crate :: sync:: { LockResult , PoisonError , TryLockError , TryLockResult , poison} ;
88use crate :: sys:: sync as sys;
99
10+
11+
1012/// A reader-writer lock
1113///
1214/// This type of lock allows a number of readers or at most one writer at any
@@ -919,19 +921,31 @@ impl<'a, T: ?Sized> RwLockReadGuard<'a, T> {
919921 }
920922 }
921923
924+ #[ unstable( feature = "rwlock_try_upgrade" , issue = "138559" ) ]
922925 pub fn try_upgrade ( orig : Self ) -> Result < RwLockWriteGuard < ' a , T > , RwLockReadGuard < ' a , T > > {
923- let read_lock = orig. inner_lock ;
926+ let rwl = & RwLock {
927+ data : UnsafeCell :: new ( orig. data . as_ptr ( ) . read ( ) ) ,
928+ poison : poison:: Flag :: new ( ) ,
929+ inner : * orig. inner_lock ,
930+ } ;
924931
925932 // don't call the destructor
926933 forget ( orig) ;
927934
928935 // SAFETY: We have ownership of the read guard, so it must be in read mode already
929- match unsafe { read_lock . try_upgrade ( ) } {
930- true => Ok ( RwLockWriteGuard :: new ( read_lock ) . unwrap_or_else ( PoisonError :: into_inner) ) ,
931- false => Err ( RwLockReadGuard :: new ( read_lock ) . unwrap_or_else ( PoisonError :: into_inner) ) ,
936+ match unsafe { rwl . inner . try_upgrade ( ) } {
937+ true => Ok ( RwLockWriteGuard :: new ( rwl ) . unwrap_or_else ( PoisonError :: into_inner) ) ,
938+ false => Err ( RwLockReadGuard :: new ( rwl ) . unwrap_or_else ( PoisonError :: into_inner) ) ,
932939 }
933940 }
934941}
942+ fn f < ' a , T > ( a : sys:: RwLock , b : NonNull < T > ) -> RwLock < T > {
943+ unsafe { RwLock {
944+ inner : a,
945+ data : UnsafeCell :: new ( b. as_ptr ( ) . read ( ) ) ,
946+ poison : poison:: Flag :: new ( ) ,
947+ } }
948+ }
935949
936950impl < ' a , T : ?Sized > MappedRwLockReadGuard < ' a , T > {
937951 /// Makes a [`MappedRwLockReadGuard`] for a component of the borrowed data,
0 commit comments