/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hyper-util-0.1.18/src/common/exec.rs
Line | Count | Source |
1 | | #![allow(dead_code)] |
2 | | |
3 | | use hyper::rt::Executor; |
4 | | use std::fmt; |
5 | | use std::future::Future; |
6 | | use std::pin::Pin; |
7 | | use std::sync::Arc; |
8 | | |
9 | | pub(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)] |
14 | | pub(crate) enum Exec { |
15 | | Executor(Arc<dyn Executor<BoxSendFuture> + Send + Sync>), |
16 | | } |
17 | | |
18 | | // ===== impl Exec ===== |
19 | | |
20 | | impl Exec { |
21 | 0 | pub(crate) fn new<E>(inner: E) -> Self |
22 | 0 | where |
23 | 0 | E: Executor<BoxSendFuture> + Send + Sync + 'static, |
24 | | { |
25 | 0 | Exec::Executor(Arc::new(inner)) |
26 | 0 | } |
27 | | |
28 | 0 | pub(crate) fn execute<F>(&self, fut: F) |
29 | 0 | where |
30 | 0 | F: Future<Output = ()> + Send + 'static, |
31 | | { |
32 | 0 | match *self { |
33 | 0 | Exec::Executor(ref e) => { |
34 | 0 | e.execute(Box::pin(fut)); |
35 | 0 | } |
36 | | } |
37 | 0 | } |
38 | | } |
39 | | |
40 | | impl fmt::Debug for Exec { |
41 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
42 | 0 | f.debug_struct("Exec").finish() |
43 | 0 | } |
44 | | } |
45 | | |
46 | | impl<F> hyper::rt::Executor<F> for Exec |
47 | | where |
48 | | F: Future<Output = ()> + Send + 'static, |
49 | | { |
50 | 0 | fn execute(&self, fut: F) { |
51 | 0 | Exec::execute(self, fut); |
52 | 0 | } |
53 | | } |