| 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 | pub(crate) fn new<E>(inner: E) -> Self |
| 22 | where |
| 23 | E: Executor<BoxSendFuture> + Send + Sync + 'static, |
| 24 | { |
| 25 | Exec::Executor(Arc::new(data: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: &Arc>> + Send + Sync + 'static>) => { |
| 34 | e.execute(fut:Box::pin(fut)); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl fmt::Debug for Exec { |
| 41 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 42 | f.debug_struct(name:"Exec" ).finish() |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl<F> hyper::rt::Executor<F> for Exec |
| 47 | where |
| 48 | F: Future<Output = ()> + Send + 'static, |
| 49 | { |
| 50 | fn execute(&self, fut: F) { |
| 51 | Exec::execute(self, fut); |
| 52 | } |
| 53 | } |
| 54 | |