1use std::future::Future;
2use std::pin::Pin;
3use std::sync::Arc;
4use std::time::Duration;
5
6use hyper::rt::{Sleep, Timer};
7
8use crate::{BoxFuture, WireError};
9
10pub trait TaskHandle: Send + Sync + 'static {
11 fn abort(&self);
12}
13
14pub type BoxTaskHandle = Box<dyn TaskHandle>;
15
16pub trait WireExecutor: Send + Sync + 'static {
17 fn spawn(&self, future: BoxFuture<()>) -> Result<BoxTaskHandle, WireError>;
18}
19
20#[derive(Clone)]
21pub struct HyperExecutor(pub Arc<dyn WireExecutor>);
22
23impl<Fut> hyper::rt::Executor<Fut> for HyperExecutor
24where
25 Fut: Future<Output = ()> + Send + 'static,
26{
27 fn execute(&self, future: Fut) {
28 if let Err(error) = self.0.spawn(Box::pin(future)) {
29 tracing::debug!(error = %error, "wire executor failed to spawn hyper future");
30 }
31 }
32}
33
34#[derive(Clone)]
35pub struct SharedTimer(pub Arc<dyn Timer + Send + Sync>);
36
37impl SharedTimer {
38 pub fn new<T>(timer: T) -> Self
39 where
40 T: Timer + Send + Sync + 'static,
41 {
42 Self(Arc::new(timer))
43 }
44}
45
46impl Timer for SharedTimer {
47 fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> {
48 self.0.sleep(duration)
49 }
50
51 fn sleep_until(&self, deadline: std::time::Instant) -> Pin<Box<dyn Sleep>> {
52 self.0.sleep_until(deadline)
53 }
54
55 fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: std::time::Instant) {
56 self.0.reset(sleep, new_deadline);
57 }
58
59 fn now(&self) -> std::time::Instant {
60 self.0.now()
61 }
62}