|
1 | 1 | //! Provides a timer trait with timer-like functions |
2 | 2 | //! |
3 | 3 | //! Example using tokio timer: |
| 4 | +//! |
4 | 5 | //! ```rust |
5 | 6 | //! use std::{ |
6 | 7 | //! future::Future, |
|
17 | 18 | //! |
18 | 19 | //! impl Timer for TokioTimer { |
19 | 20 | //! fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> { |
20 | | -//! Box::pin(TokioSleep { |
21 | | -//! inner: tokio::time::sleep(duration), |
22 | | -//! }) |
| 21 | +//! Box::pin(tokio::time::sleep(duration)) |
23 | 22 | //! } |
24 | 23 | //! |
25 | 24 | //! fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> { |
26 | | -//! Box::pin(TokioSleep { |
27 | | -//! inner: tokio::time::sleep_until(deadline.into()), |
28 | | -//! }) |
| 25 | +//! Box::pin(tokio::time::sleep_until(deadline.into())) |
29 | 26 | //! } |
30 | 27 | //! |
31 | 28 | //! fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) { |
32 | | -//! if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<TokioSleep>() { |
| 29 | +//! if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<tokio::time::Sleep>() { |
33 | 30 | //! sleep.reset(new_deadline.into()) |
34 | 31 | //! } |
35 | 32 | //! } |
36 | 33 | //! } |
37 | | -//! |
38 | | -//! pin_project! { |
39 | | -//! pub(crate) struct TokioSleep { |
40 | | -//! #[pin] |
41 | | -//! pub(crate) inner: tokio::time::Sleep, |
42 | | -//! } |
43 | | -//! } |
44 | | -//! |
45 | | -//! impl Future for TokioSleep { |
46 | | -//! type Output = (); |
47 | | -//! |
48 | | -//! fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
49 | | -//! self.project().inner.poll(cx) |
50 | | -//! } |
51 | | -//! } |
52 | | -//! |
53 | | -//! impl Sleep for TokioSleep {} |
54 | | -//! |
55 | | -//! impl TokioSleep { |
56 | | -//! pub fn reset(self: Pin<&mut Self>, deadline: Instant) { |
57 | | -//! self.project().inner.as_mut().reset(deadline.into()); |
58 | | -//! } |
59 | | -//! } |
60 | 34 | //! ```` |
61 | 35 |
|
62 | 36 | use std::{ |
|
0 commit comments