1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Provides a timer trait with timer-like functions
//!
//! Example using tokio timer:
//! ```rust
//! use std::{
//! future::Future,
//! pin::Pin,
//! task::{Context, Poll},
//! time::{Duration, Instant},
//! };
//!
//! use pin_project_lite::pin_project;
//! use hyper::rt::{Timer, Sleep};
//!
//! #[derive(Clone, Debug)]
//! pub struct TokioTimer;
//!
//! impl Timer for TokioTimer {
//! fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> {
//! Box::pin(TokioSleep {
//! inner: tokio::time::sleep(duration),
//! })
//! }
//!
//! fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> {
//! Box::pin(TokioSleep {
//! inner: tokio::time::sleep_until(deadline.into()),
//! })
//! }
//!
//! fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
//! if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<TokioSleep>() {
//! sleep.reset(new_deadline.into())
//! }
//! }
//! }
//!
//! pin_project! {
//! pub(crate) struct TokioSleep {
//! #[pin]
//! pub(crate) inner: tokio::time::Sleep,
//! }
//! }
//!
//! impl Future for TokioSleep {
//! type Output = ();
//!
//! fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
//! self.project().inner.poll(cx)
//! }
//! }
//!
//! impl Sleep for TokioSleep {}
//!
//! impl TokioSleep {
//! pub fn reset(self: Pin<&mut Self>, deadline: Instant) {
//! self.project().inner.as_mut().reset(deadline.into());
//! }
//! }
//! ```
use ;
/// A timer which provides timer-like functions.
/// A future returned by a `Timer`.