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
use crate::{Instant, TimeHandlerGuard};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

#[derive(Debug)]
pub enum Timer {
	Real(async_io::Timer),
	#[cfg(feature = "mock")]
	MockInterval(async_time_mock_core::Interval),
	// TODO: sleep, if we ever want to support the Timer methods below
}

impl From<async_io::Timer> for Timer {
	fn from(timer: async_io::Timer) -> Self {
		Self::Real(timer)
	}
}

#[cfg(feature = "mock")]
impl From<async_time_mock_core::Interval> for Timer {
	fn from(interval: async_time_mock_core::Interval) -> Self {
		Self::MockInterval(interval)
	}
}

impl Timer {
	// Timer::never can't determine if it should real or mock, therefore omitted

	// Timer::after isn't supported because it would require a TimerRegistry
	// Timer::at isn't supported because it would require a TimerRegistry
	// Timer::interval isn't supported because it would require a TimerRegistry
	// Timer::interval_at isn't supported because it would require a TimerRegistry
	// Timer::set_after isn't implemented and is unsure if will be
	// Timer::set_at isn't implemented and is unsure if will be
	// Timer::set_interval isn't implemented and is unsure if will be
	// Timer::set_interval_at isn't implemented and is unsure if will be
}

impl Future for Timer {
	type Output = (TimeHandlerGuard, Instant);

	fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
		let this = self.get_mut();
		use Timer::*;
		match this {
			Real(timer) => Pin::new(timer)
				.poll(context)
				.map(|instant| (TimeHandlerGuard::Real, instant.into())),
			#[cfg(feature = "mock")]
			MockInterval(interval) => interval
				.poll_tick(context)
				.map(|(guard, instant)| (guard.into(), instant.into())),
		}
	}
}

#[cfg(feature = "stream")]
impl futures_core::Stream for Timer {
	type Item = (TimeHandlerGuard, Instant);

	fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
		let this = self.get_mut();
		use Timer::*;
		match this {
			Real(timer) => Pin::new(timer)
				.poll_next(context)
				.map(|option| option.map(|instant| (TimeHandlerGuard::Real, instant.into()))),
			#[cfg(feature = "mock")]
			MockInterval(interval) => interval
				.poll_tick(context)
				.map(|(guard, instant)| Some((guard.into(), instant.into()))),
		}
	}

	fn size_hint(&self) -> (usize, Option<usize>) {
		(usize::MAX, None)
	}
}