futures_util/future/
with_executor.rs

1use futures_core::{Future, Poll};
2use futures_core::task;
3use futures_core::executor::Executor;
4
5/// Future for the `with_executor` combinator, assigning an executor
6/// to be used when spawning other futures.
7///
8/// This is created by the `Future::with_executor` method.
9#[derive(Debug)]
10#[must_use = "futures do nothing unless polled"]
11pub struct WithExecutor<F, E> where F: Future, E: Executor {
12    executor: E,
13    future: F
14}
15
16pub fn new<F, E>(future: F, executor: E) -> WithExecutor<F, E>
17    where F: Future,
18          E: Executor,
19{
20    WithExecutor { executor, future }
21}
22
23impl<F, E> Future for WithExecutor<F, E>
24    where F: Future,
25          E: Executor,
26{
27    type Item = F::Item;
28    type Error = F::Error;
29
30    fn poll(&mut self, cx: &mut task::Context) -> Poll<F::Item, F::Error> {
31        self.future.poll(&mut cx.with_executor(&mut self.executor))
32    }
33}