11#[ cfg( all( test, not( target_os = "emscripten" ) ) ) ]
22mod tests;
33
4- use crate :: fmt;
5- use crate :: marker;
4+ use crate :: marker:: PhantomPinned ;
65use crate :: ops:: Deref ;
76use crate :: panic:: { RefUnwindSafe , UnwindSafe } ;
7+ use crate :: pin:: Pin ;
88use crate :: sys:: mutex as sys;
99
1010/// A re-entrant mutual exclusion
@@ -15,6 +15,7 @@ use crate::sys::mutex as sys;
1515pub struct ReentrantMutex < T > {
1616 inner : sys:: ReentrantMutex ,
1717 data : T ,
18+ _pinned : PhantomPinned ,
1819}
1920
2021unsafe impl < T : Send > Send for ReentrantMutex < T > { }
@@ -37,10 +38,10 @@ impl<T> RefUnwindSafe for ReentrantMutex<T> {}
3738/// guarded data.
3839#[ must_use = "if unused the ReentrantMutex will immediately unlock" ]
3940pub struct ReentrantMutexGuard < ' a , T : ' a > {
40- lock : & ' a ReentrantMutex < T > ,
41+ lock : Pin < & ' a ReentrantMutex < T > > ,
4142}
4243
43- impl < T > !marker :: Send for ReentrantMutexGuard < ' _ , T > { }
44+ impl < T > !Send for ReentrantMutexGuard < ' _ , T > { }
4445
4546impl < T > ReentrantMutex < T > {
4647 /// Creates a new reentrant mutex in an unlocked state.
@@ -51,7 +52,11 @@ impl<T> ReentrantMutex<T> {
5152 /// once this mutex is in its final resting place, and only then are the
5253 /// lock/unlock methods safe.
5354 pub const unsafe fn new ( t : T ) -> ReentrantMutex < T > {
54- ReentrantMutex { inner : sys:: ReentrantMutex :: uninitialized ( ) , data : t }
55+ ReentrantMutex {
56+ inner : sys:: ReentrantMutex :: uninitialized ( ) ,
57+ data : t,
58+ _pinned : PhantomPinned ,
59+ }
5560 }
5661
5762 /// Initializes this mutex so it's ready for use.
@@ -60,8 +65,8 @@ impl<T> ReentrantMutex<T> {
6065 ///
6166 /// Unsafe to call more than once, and must be called after this will no
6267 /// longer move in memory.
63- pub unsafe fn init ( & self ) {
64- self . inner . init ( ) ;
68+ pub unsafe fn init ( self : Pin < & mut Self > ) {
69+ self . get_unchecked_mut ( ) . inner . init ( )
6570 }
6671
6772 /// Acquires a mutex, blocking the current thread until it is able to do so.
@@ -76,9 +81,9 @@ impl<T> ReentrantMutex<T> {
7681 /// If another user of this mutex panicked while holding the mutex, then
7782 /// this call will return failure if the mutex would otherwise be
7883 /// acquired.
79- pub fn lock ( & self ) -> ReentrantMutexGuard < ' _ , T > {
84+ pub fn lock ( self : Pin < & Self > ) -> ReentrantMutexGuard < ' _ , T > {
8085 unsafe { self . inner . lock ( ) }
81- ReentrantMutexGuard :: new ( & self )
86+ ReentrantMutexGuard { lock : self }
8287 }
8388
8489 /// Attempts to acquire this lock.
@@ -93,8 +98,12 @@ impl<T> ReentrantMutex<T> {
9398 /// If another user of this mutex panicked while holding the mutex, then
9499 /// this call will return failure if the mutex would otherwise be
95100 /// acquired.
96- pub fn try_lock ( & self ) -> Option < ReentrantMutexGuard < ' _ , T > > {
97- 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+ }
98107 }
99108}
100109
@@ -107,30 +116,6 @@ impl<T> Drop for ReentrantMutex<T> {
107116 }
108117}
109118
110- impl < T : fmt:: Debug + ' static > fmt:: Debug for ReentrantMutex < T > {
111- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
112- match self . try_lock ( ) {
113- Some ( guard) => f. debug_struct ( "ReentrantMutex" ) . field ( "data" , & * guard) . finish ( ) ,
114- None => {
115- struct LockedPlaceholder ;
116- impl fmt:: Debug for LockedPlaceholder {
117- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
118- f. write_str ( "<locked>" )
119- }
120- }
121-
122- f. debug_struct ( "ReentrantMutex" ) . field ( "data" , & LockedPlaceholder ) . finish ( )
123- }
124- }
125- }
126- }
127-
128- impl < ' mutex , T > ReentrantMutexGuard < ' mutex , T > {
129- fn new ( lock : & ' mutex ReentrantMutex < T > ) -> ReentrantMutexGuard < ' mutex , T > {
130- ReentrantMutexGuard { lock }
131- }
132- }
133-
134119impl < T > Deref for ReentrantMutexGuard < ' _ , T > {
135120 type Target = T ;
136121
0 commit comments