async_time_mock_async_std/
interval.rs

1use super::TimeHandlerGuard;
2use async_std::stream::Stream;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6#[derive(Debug)]
7pub enum Interval {
8	Real(async_std::stream::Interval),
9	#[cfg(feature = "mock")]
10	Mock(async_time_mock_core::Interval),
11}
12
13impl From<async_std::stream::Interval> for Interval {
14	fn from(interval: async_std::stream::Interval) -> Self {
15		Self::Real(interval)
16	}
17}
18
19#[cfg(feature = "mock")]
20impl From<async_time_mock_core::Interval> for Interval {
21	fn from(interval: async_time_mock_core::Interval) -> Self {
22		Self::Mock(interval)
23	}
24}
25
26impl Stream for Interval {
27	type Item = TimeHandlerGuard;
28
29	fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
30		let this = self.get_mut();
31		use Interval::*;
32		match this {
33			Real(interval) => Pin::new(interval)
34				.poll_next(context)
35				.map(|option| option.map(|_| TimeHandlerGuard::Real)),
36			#[cfg(feature = "mock")]
37			Mock(interval) => interval.poll_tick(context).map(|(guard, _)| Some(guard.into())),
38		}
39	}
40
41	fn size_hint(&self) -> (usize, Option<usize>) {
42		(usize::MAX, None)
43	}
44}