| 19 | |
| 20 | impl Executor for ThreadPool { |
| 21 | fn defer<F, T>(&self, f: F) -> Future<T> |
| 22 | where |
| 23 | F: FnOnce() -> T + Send + 'static, |
| 24 | T: Send + Sync + 'static, |
| 25 | { |
| 26 | let sender_future = Future::pending(); |
| 27 | let receiver_future = sender_future.clone(); |
| 28 | |
| 29 | self.spawn(move || { |
| 30 | // Create the guard panic first, then run the closure. The guard will be dropped at the |
| 31 | // end of this scope. If the function succeeds, the guard won't do anything; if it |
| 32 | // panics, the guard's `Drop` implementation will poison the future. |
| 33 | let _guard = PanicGuard { future: &sender_future }; |
| 34 | sender_future.complete(f()) |
| 35 | }); |
| 36 | |
| 37 | receiver_future |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// A "guard" to detect if this thread panics, and poison the `Future` in that case. |