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
use super::TimeHandlerGuard;
use async_std::stream::Stream;
use std::pin::Pin;
use std::task::{Context, Poll};

#[derive(Debug)]
pub enum Interval {
	Real(async_std::stream::Interval),
	#[cfg(feature = "mock")]
	Mock(async_time_mock_core::Interval),
}

impl From<async_std::stream::Interval> for Interval {
	fn from(interval: async_std::stream::Interval) -> Self {
		Self::Real(interval)
	}
}

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

impl Stream for Interval {
	type Item = TimeHandlerGuard;

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

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