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
use std::future::Future;
use std::time::Duration;
#[derive(Clone, Debug)]
pub enum Runtime {
None,
#[cfg(feature = "rt_tokio_1")]
Tokio1,
#[cfg(feature = "rt_async-std_1")]
AsyncStd1,
}
impl Default for Runtime {
fn default() -> Self {
Runtime::None
}
}
pub enum TimeoutError {
Timeout,
NoRuntime,
}
impl Runtime {
#[allow(unused_variables)]
pub async fn timeout<F>(&self, duration: Duration, future: F) -> Result<F::Output, TimeoutError>
where
F: Future,
{
match self {
Self::None => Err(TimeoutError::NoRuntime),
#[cfg(feature = "rt_tokio_1")]
Self::Tokio1 => tokio::time::timeout(duration, future)
.await
.map_err(|_| TimeoutError::Timeout),
#[cfg(feature = "rt_async-std_1")]
Self::AsyncStd1 => async_std::future::timeout(duration, future)
.await
.map_err(|_| TimeoutError::Timeout),
}
}
}