12
12
reason = "futures in libcore are unstable" ,
13
13
issue = "50547" ) ]
14
14
15
- use { fmt, mem } ;
15
+ use fmt;
16
16
use marker:: Unpin ;
17
17
use ptr:: NonNull ;
18
18
@@ -63,6 +63,20 @@ impl Waker {
63
63
pub fn will_wake ( & self , other : & Waker ) -> bool {
64
64
self . inner == other. inner
65
65
}
66
+
67
+ /// Returns whether or not this `Waker` and `other` `LocalWaker` awaken
68
+ /// the same task.
69
+ ///
70
+ /// This function works on a best-effort basis, and may return false even
71
+ /// when the `Waker`s would awaken the same task. However, if this function
72
+ /// returns true, it is guaranteed that the `Waker`s will awaken the same
73
+ /// task.
74
+ ///
75
+ /// This function is primarily used for optimization purposes.
76
+ #[ inline]
77
+ pub fn will_wake_local ( & self , other : & LocalWaker ) -> bool {
78
+ self . will_wake ( & other. 0 )
79
+ }
66
80
}
67
81
68
82
impl Clone for Waker {
@@ -97,9 +111,8 @@ impl Drop for Waker {
97
111
/// Task executors can use this type to implement more optimized singlethreaded wakeup
98
112
/// behavior.
99
113
#[ repr( transparent) ]
100
- pub struct LocalWaker {
101
- inner : NonNull < dyn UnsafeWake > ,
102
- }
114
+ #[ derive( Clone ) ]
115
+ pub struct LocalWaker ( Waker ) ;
103
116
104
117
impl Unpin for LocalWaker { }
105
118
impl !Send for LocalWaker { }
@@ -120,7 +133,16 @@ impl LocalWaker {
120
133
/// on the current thread.
121
134
#[ inline]
122
135
pub unsafe fn new ( inner : NonNull < dyn UnsafeWake > ) -> Self {
123
- LocalWaker { inner }
136
+ LocalWaker ( Waker :: new ( inner) )
137
+ }
138
+
139
+ /// Borrows this `LocalWaker` as a `Waker`.
140
+ ///
141
+ /// `Waker` is nearly identical to `LocalWaker`, but is threadsafe
142
+ /// (implements `Send` and `Sync`).
143
+ #[ inline]
144
+ pub fn as_waker ( & self ) -> & Waker {
145
+ & self . 0
124
146
}
125
147
126
148
/// Converts this `LocalWaker` into a `Waker`.
@@ -129,13 +151,13 @@ impl LocalWaker {
129
151
/// (implements `Send` and `Sync`).
130
152
#[ inline]
131
153
pub fn into_waker ( self ) -> Waker {
132
- self . into ( )
154
+ self . 0
133
155
}
134
156
135
157
/// Wake up the task associated with this `LocalWaker`.
136
158
#[ inline]
137
159
pub fn wake ( & self ) {
138
- unsafe { self . inner . as_ref ( ) . wake_local ( ) }
160
+ unsafe { self . 0 . inner . as_ref ( ) . wake_local ( ) }
139
161
}
140
162
141
163
/// Returns whether or not this `LocalWaker` and `other` `LocalWaker` awaken the same task.
@@ -148,7 +170,7 @@ impl LocalWaker {
148
170
/// This function is primarily used for optimization purposes.
149
171
#[ inline]
150
172
pub fn will_wake ( & self , other : & LocalWaker ) -> bool {
151
- self . inner == other. inner
173
+ self . 0 . will_wake ( & other. 0 )
152
174
}
153
175
154
176
/// Returns whether or not this `LocalWaker` and `other` `Waker` awaken the same task.
@@ -161,45 +183,24 @@ impl LocalWaker {
161
183
/// This function is primarily used for optimization purposes.
162
184
#[ inline]
163
185
pub fn will_wake_nonlocal ( & self , other : & Waker ) -> bool {
164
- self . inner == other . inner
186
+ self . 0 . will_wake ( other )
165
187
}
166
188
}
167
189
168
190
impl From < LocalWaker > for Waker {
169
191
#[ inline]
170
192
fn from ( local_waker : LocalWaker ) -> Self {
171
- let inner = local_waker. inner ;
172
- mem:: forget ( local_waker) ;
173
- Waker { inner }
174
- }
175
- }
176
-
177
- impl Clone for LocalWaker {
178
- #[ inline]
179
- fn clone ( & self ) -> Self {
180
- let waker = unsafe { self . inner . as_ref ( ) . clone_raw ( ) } ;
181
- let inner = waker. inner ;
182
- mem:: forget ( waker) ;
183
- LocalWaker { inner }
193
+ local_waker. 0
184
194
}
185
195
}
186
196
187
197
impl fmt:: Debug for LocalWaker {
188
198
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
189
- f. debug_struct ( "Waker " )
199
+ f. debug_struct ( "LocalWaker " )
190
200
. finish ( )
191
201
}
192
202
}
193
203
194
- impl Drop for LocalWaker {
195
- #[ inline]
196
- fn drop ( & mut self ) {
197
- unsafe {
198
- self . inner . as_ref ( ) . drop_raw ( )
199
- }
200
- }
201
- }
202
-
203
204
/// An unsafe trait for implementing custom memory management for a `Waker` or `LocalWaker`.
204
205
///
205
206
/// A `Waker` conceptually is a cloneable trait object for `Wake`, and is
0 commit comments