@@ -69,6 +69,7 @@ impl Notifier {
69
69
} else {
70
70
let state = Arc :: new ( Mutex :: new ( FutureState {
71
71
callbacks : Vec :: new ( ) ,
72
+ std_future_callbacks : Vec :: new ( ) ,
72
73
callbacks_with_state : Vec :: new ( ) ,
73
74
complete : lock. 0 ,
74
75
callbacks_made : false ,
@@ -109,25 +110,29 @@ define_callback!(Send);
109
110
define_callback ! ( ) ;
110
111
111
112
pub ( crate ) struct FutureState {
112
- // When we're tracking whether a callback counts as having woken the user's code, we check the
113
- // first bool - set to false if we're just calling a Waker, and true if we're calling an actual
114
- // user-provided function.
115
- callbacks : Vec < ( bool , Box < dyn FutureCallback > ) > ,
116
- callbacks_with_state : Vec < ( bool , Box < dyn Fn ( & Arc < Mutex < FutureState > > ) -> ( ) + Send > ) > ,
113
+ // `callbacks` count as having woken the users' code (as they go direct to the user), but
114
+ // `std_future_callbacks` and `callbacks_with_state` do not (as the first just wakes a future,
115
+ // we only count it after another `poll()` and the second wakes a `Sleeper` which handles
116
+ // setting `callbacks_made` itself).
117
+ callbacks : Vec < Box < dyn FutureCallback > > ,
118
+ std_future_callbacks : Vec < StdWaker > ,
119
+ callbacks_with_state : Vec < Box < dyn Fn ( & Arc < Mutex < FutureState > > ) -> ( ) + Send > > ,
117
120
complete : bool ,
118
121
callbacks_made : bool ,
119
122
}
120
123
121
124
fn complete_future ( this : & Arc < Mutex < FutureState > > ) -> bool {
122
125
let mut state_lock = this. lock ( ) . unwrap ( ) ;
123
126
let state = & mut * state_lock;
124
- for ( counts_as_call , callback) in state. callbacks . drain ( ..) {
127
+ for callback in state. callbacks . drain ( ..) {
125
128
callback. call ( ) ;
126
- state. callbacks_made |= counts_as_call ;
129
+ state. callbacks_made = true ;
127
130
}
128
- for ( counts_as_call, callback) in state. callbacks_with_state . drain ( ..) {
131
+ for waker in state. std_future_callbacks . drain ( ..) {
132
+ waker. 0 . wake_by_ref ( ) ;
133
+ }
134
+ for callback in state. callbacks_with_state . drain ( ..) {
129
135
( callback) ( this) ;
130
- state. callbacks_made |= counts_as_call;
131
136
}
132
137
state. complete = true ;
133
138
state. callbacks_made
@@ -153,7 +158,7 @@ impl Future {
153
158
mem:: drop ( state) ;
154
159
callback. call ( ) ;
155
160
} else {
156
- state. callbacks . push ( ( true , callback) ) ;
161
+ state. callbacks . push ( callback) ;
157
162
}
158
163
}
159
164
@@ -193,9 +198,6 @@ impl Future {
193
198
194
199
use core:: task:: Waker ;
195
200
struct StdWaker ( pub Waker ) ;
196
- impl FutureCallback for StdWaker {
197
- fn call ( & self ) { self . 0 . wake_by_ref ( ) }
198
- }
199
201
200
202
/// This is not exported to bindings users as Rust Futures aren't usable in language bindings.
201
203
impl < ' a > StdFuture for Future {
@@ -208,7 +210,7 @@ impl<'a> StdFuture for Future {
208
210
Poll :: Ready ( ( ) )
209
211
} else {
210
212
let waker = cx. waker ( ) . clone ( ) ;
211
- state. callbacks . push ( ( false , Box :: new ( StdWaker ( waker) ) ) ) ;
213
+ state. std_future_callbacks . push ( StdWaker ( waker) ) ;
212
214
Poll :: Pending
213
215
}
214
216
}
@@ -251,10 +253,10 @@ impl Sleeper {
251
253
* notified_fut_mtx. lock ( ) . unwrap ( ) = Some ( Arc :: clone ( & notifier_mtx) ) ;
252
254
break ;
253
255
}
254
- notifier. callbacks_with_state . push ( ( false , Box :: new ( move |notifier_ref| {
256
+ notifier. callbacks_with_state . push ( Box :: new ( move |notifier_ref| {
255
257
* notified_fut_ref. lock ( ) . unwrap ( ) = Some ( Arc :: clone ( notifier_ref) ) ;
256
258
cv_ref. notify_all ( ) ;
257
- } ) ) ) ;
259
+ } ) ) ;
258
260
}
259
261
}
260
262
( cv, notified_fut_mtx)
@@ -455,6 +457,7 @@ mod tests {
455
457
let future = Future {
456
458
state : Arc :: new ( Mutex :: new ( FutureState {
457
459
callbacks : Vec :: new ( ) ,
460
+ std_future_callbacks : Vec :: new ( ) ,
458
461
callbacks_with_state : Vec :: new ( ) ,
459
462
complete : false ,
460
463
callbacks_made : false ,
@@ -514,6 +517,7 @@ mod tests {
514
517
let mut future = Future {
515
518
state : Arc :: new ( Mutex :: new ( FutureState {
516
519
callbacks : Vec :: new ( ) ,
520
+ std_future_callbacks : Vec :: new ( ) ,
517
521
callbacks_with_state : Vec :: new ( ) ,
518
522
complete : false ,
519
523
callbacks_made : false ,
0 commit comments