hyper_util/common/
exec.rs

1#![allow(dead_code)]
2
3use hyper::rt::Executor;
4use std::fmt;
5use std::future::Future;
6use std::pin::Pin;
7use std::sync::Arc;
8
9pub(crate) type BoxSendFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
10
11// Either the user provides an executor for background tasks, or we use
12// `tokio::spawn`.
13#[derive(Clone)]
14pub(crate) enum Exec {
15    Executor(Arc<dyn Executor<BoxSendFuture> + Send + Sync>),
16}
17
18// ===== impl Exec =====
19
20impl Exec {
21    pub(crate) fn new<E>(inner: E) -> Self
22    where
23        E: Executor<BoxSendFuture> + Send + Sync + 'static,
24    {
25        Exec::Executor(Arc::new(inner))
26    }
27
28    pub(crate) fn execute<F>(&self, fut: F)
29    where
30        F: Future<Output = ()> + Send + 'static,
31    {
32        match *self {
33            Exec::Executor(ref e) => {
34                e.execute(Box::pin(fut));
35            }
36        }
37    }
38}
39
40impl fmt::Debug for Exec {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        f.debug_struct("Exec").finish()
43    }
44}
45
46impl<F> hyper::rt::Executor<F> for Exec
47where
48    F: Future<Output = ()> + Send + 'static,
49{
50    fn execute(&self, fut: F) {
51        Exec::execute(self, fut);
52    }
53}