1
1
#[ cfg( all( test, not( target_os = "emscripten" ) ) ) ]
2
2
mod tests;
3
3
4
- use crate :: fmt ;
4
+ use crate :: marker :: PhantomPinned ;
5
5
use crate :: ops:: Deref ;
6
6
use crate :: panic:: { RefUnwindSafe , UnwindSafe } ;
7
+ use crate :: pin:: Pin ;
7
8
use crate :: sys:: mutex as sys;
8
9
9
10
/// A re-entrant mutual exclusion
@@ -14,6 +15,7 @@ use crate::sys::mutex as sys;
14
15
pub struct ReentrantMutex < T > {
15
16
inner : sys:: ReentrantMutex ,
16
17
data : T ,
18
+ _pinned : PhantomPinned ,
17
19
}
18
20
19
21
unsafe impl < T : Send > Send for ReentrantMutex < T > { }
@@ -36,7 +38,7 @@ impl<T> RefUnwindSafe for ReentrantMutex<T> {}
36
38
/// guarded data.
37
39
#[ must_use = "if unused the ReentrantMutex will immediately unlock" ]
38
40
pub struct ReentrantMutexGuard < ' a , T : ' a > {
39
- lock : & ' a ReentrantMutex < T > ,
41
+ lock : Pin < & ' a ReentrantMutex < T > > ,
40
42
}
41
43
42
44
impl < T > !Send for ReentrantMutexGuard < ' _ , T > { }
@@ -50,7 +52,11 @@ impl<T> ReentrantMutex<T> {
50
52
/// once this mutex is in its final resting place, and only then are the
51
53
/// lock/unlock methods safe.
52
54
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
+ }
54
60
}
55
61
56
62
/// Initializes this mutex so it's ready for use.
@@ -59,8 +65,8 @@ impl<T> ReentrantMutex<T> {
59
65
///
60
66
/// Unsafe to call more than once, and must be called after this will no
61
67
/// 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 ( )
64
70
}
65
71
66
72
/// Acquires a mutex, blocking the current thread until it is able to do so.
@@ -75,9 +81,9 @@ impl<T> ReentrantMutex<T> {
75
81
/// If another user of this mutex panicked while holding the mutex, then
76
82
/// this call will return failure if the mutex would otherwise be
77
83
/// acquired.
78
- pub fn lock ( & self ) -> ReentrantMutexGuard < ' _ , T > {
84
+ pub fn lock ( self : Pin < & Self > ) -> ReentrantMutexGuard < ' _ , T > {
79
85
unsafe { self . inner . lock ( ) }
80
- ReentrantMutexGuard :: new ( & self )
86
+ ReentrantMutexGuard { lock : self }
81
87
}
82
88
83
89
/// Attempts to acquire this lock.
@@ -92,8 +98,12 @@ impl<T> ReentrantMutex<T> {
92
98
/// If another user of this mutex panicked while holding the mutex, then
93
99
/// this call will return failure if the mutex would otherwise be
94
100
/// 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
+ }
97
107
}
98
108
}
99
109
@@ -106,30 +116,6 @@ impl<T> Drop for ReentrantMutex<T> {
106
116
}
107
117
}
108
118
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
-
133
119
impl < T > Deref for ReentrantMutexGuard < ' _ , T > {
134
120
type Target = T ;
135
121
0 commit comments