1+ #[ cfg( feature = "threading" ) ]
12use parking_lot:: { Mutex , MutexGuard , RwLock , RwLockReadGuard , RwLockWriteGuard } ;
3+ #[ cfg( not( feature = "threading" ) ) ]
4+ use std:: cell:: { Ref , RefCell , RefMut } ;
25use std:: ops:: { Deref , DerefMut } ;
36
7+ cfg_if:: cfg_if! {
8+ if #[ cfg( feature = "threading" ) ] {
9+ type MutexInner <T > = Mutex <T >;
10+ type MutexGuardInner <' a, T > = MutexGuard <' a, T >;
11+ const fn new_mutex<T >( value: T ) -> MutexInner <T > {
12+ parking_lot:: const_mutex( value)
13+ }
14+ fn lock_mutex<T : ?Sized >( m: & MutexInner <T >) -> MutexGuardInner <T > {
15+ m. lock( )
16+ }
17+ } else {
18+ type MutexInner <T > = RefCell <T >;
19+ type MutexGuardInner <' a, T > = RefMut <' a, T >;
20+ const fn new_mutex<T >( value: T ) -> MutexInner <T > {
21+ RefCell :: new( value)
22+ }
23+ fn lock_mutex<T : ?Sized >( m: & MutexInner <T >) -> MutexGuardInner <T > {
24+ m. borrow_mut( )
25+ }
26+ }
27+ }
28+
429#[ derive( Debug , Default ) ]
530#[ repr( transparent) ]
6- pub struct PyMutex < T : ?Sized > ( Mutex < T > ) ;
31+ pub struct PyMutex < T : ?Sized > ( MutexInner < T > ) ;
732
833impl < T > PyMutex < T > {
934 pub const fn new ( value : T ) -> Self {
10- Self ( parking_lot :: const_mutex ( value) )
35+ Self ( new_mutex ( value) )
1136 }
1237}
1338
1439impl < T : ?Sized > PyMutex < T > {
1540 pub fn lock ( & self ) -> PyMutexGuard < T > {
16- PyMutexGuard ( self . 0 . lock ( ) )
41+ PyMutexGuard ( lock_mutex ( & self . 0 ) )
1742 }
1843}
1944
2045#[ derive( Debug ) ]
2146#[ repr( transparent) ]
22- pub struct PyMutexGuard < ' a , T : ?Sized > ( MutexGuard < ' a , T > ) ;
47+ pub struct PyMutexGuard < ' a , T : ?Sized > ( MutexGuardInner < ' a , T > ) ;
2348impl < T : ?Sized > Deref for PyMutexGuard < ' _ , T > {
2449 type Target = T ;
2550 fn deref ( & self ) -> & T {
@@ -32,28 +57,58 @@ impl<T: ?Sized> DerefMut for PyMutexGuard<'_, T> {
3257 }
3358}
3459
60+ cfg_if:: cfg_if! {
61+ if #[ cfg( feature = "threading" ) ] {
62+ type RwLockInner <T > = RwLock <T >;
63+ type RwLockReadInner <' a, T > = RwLockReadGuard <' a, T >;
64+ type RwLockWriteInner <' a, T > = RwLockWriteGuard <' a, T >;
65+ const fn new_rwlock<T >( value: T ) -> RwLockInner <T > {
66+ parking_lot:: const_rwlock( value)
67+ }
68+ fn read_rwlock<T : ?Sized >( m: & RwLockInner <T >) -> RwLockReadInner <T > {
69+ m. read( )
70+ }
71+ fn write_rwlock<T : ?Sized >( m: & RwLockInner <T >) -> RwLockWriteInner <T > {
72+ m. write( )
73+ }
74+ } else {
75+ type RwLockInner <T > = RefCell <T >;
76+ type RwLockReadInner <' a, T > = Ref <' a, T >;
77+ type RwLockWriteInner <' a, T > = RefMut <' a, T >;
78+ const fn new_rwlock<T >( value: T ) -> RwLockInner <T > {
79+ RefCell :: new( value)
80+ }
81+ fn read_rwlock<T : ?Sized >( m: & RwLockInner <T >) -> RwLockReadInner <T > {
82+ m. borrow( )
83+ }
84+ fn write_rwlock<T : ?Sized >( m: & RwLockInner <T >) -> RwLockWriteInner <T > {
85+ m. borrow_mut( )
86+ }
87+ }
88+ }
89+
3590#[ derive( Debug , Default ) ]
3691#[ repr( transparent) ]
37- pub struct PyRwLock < T : ?Sized > ( RwLock < T > ) ;
92+ pub struct PyRwLock < T : ?Sized > ( RwLockInner < T > ) ;
3893
3994impl < T > PyRwLock < T > {
4095 pub const fn new ( value : T ) -> Self {
41- Self ( parking_lot :: const_rwlock ( value) )
96+ Self ( new_rwlock ( value) )
4297 }
4398}
4499
45100impl < T : ?Sized > PyRwLock < T > {
46101 pub fn read ( & self ) -> PyRwLockReadGuard < T > {
47- PyRwLockReadGuard ( self . 0 . read ( ) )
102+ PyRwLockReadGuard ( read_rwlock ( & self . 0 ) )
48103 }
49104 pub fn write ( & self ) -> PyRwLockWriteGuard < T > {
50- PyRwLockWriteGuard ( self . 0 . write ( ) )
105+ PyRwLockWriteGuard ( write_rwlock ( & self . 0 ) )
51106 }
52107}
53108
54109#[ derive( Debug ) ]
55110#[ repr( transparent) ]
56- pub struct PyRwLockReadGuard < ' a , T : ?Sized > ( RwLockReadGuard < ' a , T > ) ;
111+ pub struct PyRwLockReadGuard < ' a , T : ?Sized > ( RwLockReadInner < ' a , T > ) ;
57112impl < T : ?Sized > Deref for PyRwLockReadGuard < ' _ , T > {
58113 type Target = T ;
59114 fn deref ( & self ) -> & T {
@@ -63,7 +118,7 @@ impl<T: ?Sized> Deref for PyRwLockReadGuard<'_, T> {
63118
64119#[ derive( Debug ) ]
65120#[ repr( transparent) ]
66- pub struct PyRwLockWriteGuard < ' a , T : ?Sized > ( RwLockWriteGuard < ' a , T > ) ;
121+ pub struct PyRwLockWriteGuard < ' a , T : ?Sized > ( RwLockWriteInner < ' a , T > ) ;
67122impl < T : ?Sized > Deref for PyRwLockWriteGuard < ' _ , T > {
68123 type Target = T ;
69124 fn deref ( & self ) -> & T {
0 commit comments