Skip to main content

hyper_util/common/
exec.rs

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