@@ -3,10 +3,12 @@ use crate::fmt;
3
3
use crate :: marker:: PhantomData ;
4
4
use crate :: mem:: { self , ManuallyDrop , forget} ;
5
5
use crate :: ops:: { Deref , DerefMut } ;
6
- use crate :: ptr:: NonNull ;
6
+ use crate :: ptr:: { NonNull } ;
7
7
use crate :: sync:: { LockResult , PoisonError , TryLockError , TryLockResult , poison} ;
8
8
use crate :: sys:: sync as sys;
9
9
10
+
11
+
10
12
/// A reader-writer lock
11
13
///
12
14
/// 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> {
919
921
}
920
922
}
921
923
924
+ #[ unstable( feature = "rwlock_try_upgrade" , issue = "138559" ) ]
922
925
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
+ } ;
924
931
925
932
// don't call the destructor
926
933
forget ( orig) ;
927
934
928
935
// 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) ) ,
932
939
}
933
940
}
934
941
}
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
+ }
935
949
936
950
impl < ' a , T : ?Sized > MappedRwLockReadGuard < ' a , T > {
937
951
/// Makes a [`MappedRwLockReadGuard`] for a component of the borrowed data,
0 commit comments