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
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};

// NOTE That the can't implement PartialEq, Eq, Clone or Copy, because TimeHandlerGuard doesn't support that.
#[must_use = "TimeoutError must only be dropped once all side-effects of the timeout have been handled."]
#[derive(Debug)]
pub enum TimeoutError {
	Real(async_std::future::TimeoutError),
	#[cfg(feature = "mock")]
	Mock(async_time_mock_core::Elapsed),
}

impl Display for TimeoutError {
	fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
		use TimeoutError::*;
		match self {
			Real(error) => Display::fmt(error, formatter),
			#[cfg(feature = "mock")]
			Mock(elapsed) => Display::fmt(elapsed, formatter),
		}
	}
}

impl Error for TimeoutError {}

impl From<async_std::future::TimeoutError> for TimeoutError {
	fn from(error: async_std::future::TimeoutError) -> Self {
		Self::Real(error)
	}
}

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