File tree 1 file changed +17
-1
lines changed
1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ use std::{
2
2
future:: Future ,
3
3
marker:: PhantomData ,
4
4
mem,
5
+ pin:: Pin ,
5
6
sync:: Arc ,
6
7
thread:: { self , JoinHandle } ,
7
8
} ;
@@ -279,8 +280,23 @@ impl TaskPool {
279
280
// Pin the futures on the stack.
280
281
pin ! ( get_results) ;
281
282
283
+ // SAFETY: This function blocks until all futures complete, so we do not read/write
284
+ // the data from futures outside of the 'scope lifetime. However,
285
+ // rust has no way of knowing this so we must convert to 'static
286
+ // here to appease the compiler as it is unable to validate safety.
287
+ let get_results: Pin < & mut ( dyn Future < Output = Vec < T > > + ' static + Send ) > = get_results;
288
+ let get_results: Pin < & ' static mut ( dyn Future < Output = Vec < T > > + ' static + Send ) > =
289
+ unsafe { mem:: transmute ( get_results) } ;
290
+
291
+ // The thread that calls scope() will participate in driving tasks in the pool
292
+ // forward until the tasks that are spawned by this scope() call
293
+ // complete. (If the caller of scope() happens to be a thread in
294
+ // this thread pool, and we only have one thread in the pool, then
295
+ // simply calling future::block_on(spawned) would deadlock.)
296
+ let mut spawned = task_scope_executor. spawn ( get_results) ;
297
+
282
298
loop {
283
- if let Some ( result) = future:: block_on ( future:: poll_once ( & mut get_results ) ) {
299
+ if let Some ( result) = future:: block_on ( future:: poll_once ( & mut spawned ) ) {
284
300
break result;
285
301
} ;
286
302
You can’t perform that action at this time.
0 commit comments