async_time_mock_smol/
timer.rs1use crate::{Instant, TimeHandlerGuard};
2use std::future::Future;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6#[derive(Debug)]
7pub enum Timer {
8 Real(async_io::Timer),
9 #[cfg(feature = "mock")]
10 MockInterval(async_time_mock_core::Interval),
11 }
13
14impl From<async_io::Timer> for Timer {
15 fn from(timer: async_io::Timer) -> Self {
16 Self::Real(timer)
17 }
18}
19
20#[cfg(feature = "mock")]
21impl From<async_time_mock_core::Interval> for Timer {
22 fn from(interval: async_time_mock_core::Interval) -> Self {
23 Self::MockInterval(interval)
24 }
25}
26
27impl Timer {
28 }
39
40impl Future for Timer {
41 type Output = (TimeHandlerGuard, Instant);
42
43 fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
44 let this = self.get_mut();
45 use Timer::*;
46 match this {
47 Real(timer) => Pin::new(timer)
48 .poll(context)
49 .map(|instant| (TimeHandlerGuard::Real, instant.into())),
50 #[cfg(feature = "mock")]
51 MockInterval(interval) => interval
52 .poll_tick(context)
53 .map(|(guard, instant)| (guard.into(), instant.into())),
54 }
55 }
56}
57
58#[cfg(feature = "stream")]
59impl futures_core::Stream for Timer {
60 type Item = (TimeHandlerGuard, Instant);
61
62 fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
63 let this = self.get_mut();
64 use Timer::*;
65 match this {
66 Real(timer) => Pin::new(timer)
67 .poll_next(context)
68 .map(|option| option.map(|instant| (TimeHandlerGuard::Real, instant.into()))),
69 #[cfg(feature = "mock")]
70 MockInterval(interval) => interval
71 .poll_tick(context)
72 .map(|(guard, instant)| Some((guard.into(), instant.into()))),
73 }
74 }
75
76 fn size_hint(&self) -> (usize, Option<usize>) {
77 (usize::MAX, None)
78 }
79}