11#[ cfg( all( test, not( target_os = "emscripten" ) ) ) ]
22mod tests;
33
4- use crate :: fmt ;
4+ use crate :: marker :: PhantomPinned ;
55use crate :: ops:: Deref ;
66use crate :: panic:: { RefUnwindSafe , UnwindSafe } ;
7+ use crate :: pin:: Pin ;
78use crate :: sys:: mutex as sys;
89
910/// A re-entrant mutual exclusion
@@ -14,6 +15,7 @@ use crate::sys::mutex as sys;
1415pub struct ReentrantMutex < T > {
1516 inner : sys:: ReentrantMutex ,
1617 data : T ,
18+ _pinned : PhantomPinned ,
1719}
1820
1921unsafe impl < T : Send > Send for ReentrantMutex < T > { }
@@ -36,7 +38,7 @@ impl<T> RefUnwindSafe for ReentrantMutex<T> {}
3638/// guarded data.
3739#[ must_use = "if unused the ReentrantMutex will immediately unlock" ]
3840pub struct ReentrantMutexGuard < ' a , T : ' a > {
39- lock : & ' a ReentrantMutex < T > ,
41+ lock : Pin < & ' a ReentrantMutex < T > > ,
4042}
4143
4244impl < T > !Send for ReentrantMutexGuard < ' _ , T > { }
@@ -50,7 +52,11 @@ impl<T> ReentrantMutex<T> {
5052 /// once this mutex is in its final resting place, and only then are the
5153 /// lock/unlock methods safe.
5254 pub const unsafe fn new ( t : T ) -> ReentrantMutex < T > {
53- ReentrantMutex { inner : sys:: ReentrantMutex :: uninitialized ( ) , data : t }
55+ ReentrantMutex {
56+ inner : sys:: ReentrantMutex :: uninitialized ( ) ,
57+ data : t,
58+ _pinned : PhantomPinned ,
59+ }
5460 }
5561
5662 /// Initializes this mutex so it's ready for use.
@@ -59,8 +65,8 @@ impl<T> ReentrantMutex<T> {
5965 ///
6066 /// Unsafe to call more than once, and must be called after this will no
6167 /// longer move in memory.
62- pub unsafe fn init ( & self ) {
63- self . inner . init ( ) ;
68+ pub unsafe fn init ( self : Pin < & mut Self > ) {
69+ self . get_unchecked_mut ( ) . inner . init ( )
6470 }
6571
6672 /// Acquires a mutex, blocking the current thread until it is able to do so.
@@ -75,9 +81,9 @@ impl<T> ReentrantMutex<T> {
7581 /// If another user of this mutex panicked while holding the mutex, then
7682 /// this call will return failure if the mutex would otherwise be
7783 /// acquired.
78- pub fn lock ( & self ) -> ReentrantMutexGuard < ' _ , T > {
84+ pub fn lock ( self : Pin < & Self > ) -> ReentrantMutexGuard < ' _ , T > {
7985 unsafe { self . inner . lock ( ) }
80- ReentrantMutexGuard :: new ( & self )
86+ ReentrantMutexGuard { lock : self }
8187 }
8288
8389 /// Attempts to acquire this lock.
@@ -92,8 +98,12 @@ impl<T> ReentrantMutex<T> {
9298 /// If another user of this mutex panicked while holding the mutex, then
9399 /// this call will return failure if the mutex would otherwise be
94100 /// acquired.
95- pub fn try_lock ( & self ) -> Option < ReentrantMutexGuard < ' _ , T > > {
96- if unsafe { self . inner . try_lock ( ) } { Some ( ReentrantMutexGuard :: new ( & self ) ) } else { None }
101+ pub fn try_lock ( self : Pin < & Self > ) -> Option < ReentrantMutexGuard < ' _ , T > > {
102+ if unsafe { self . inner . try_lock ( ) } {
103+ Some ( ReentrantMutexGuard { lock : self } )
104+ } else {
105+ None
106+ }
97107 }
98108}
99109
@@ -106,30 +116,6 @@ impl<T> Drop for ReentrantMutex<T> {
106116 }
107117}
108118
109- impl < T : fmt:: Debug + ' static > fmt:: Debug for ReentrantMutex < T > {
110- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
111- match self . try_lock ( ) {
112- Some ( guard) => f. debug_struct ( "ReentrantMutex" ) . field ( "data" , & * guard) . finish ( ) ,
113- None => {
114- struct LockedPlaceholder ;
115- impl fmt:: Debug for LockedPlaceholder {
116- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
117- f. write_str ( "<locked>" )
118- }
119- }
120-
121- f. debug_struct ( "ReentrantMutex" ) . field ( "data" , & LockedPlaceholder ) . finish ( )
122- }
123- }
124- }
125- }
126-
127- impl < ' mutex , T > ReentrantMutexGuard < ' mutex , T > {
128- fn new ( lock : & ' mutex ReentrantMutex < T > ) -> ReentrantMutexGuard < ' mutex , T > {
129- ReentrantMutexGuard { lock }
130- }
131- }
132-
133119impl < T > Deref for ReentrantMutexGuard < ' _ , T > {
134120 type Target = T ;
135121
0 commit comments