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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use alloc::boxed::Box;
use core::{future::Future, time::Duration};

use futures_util::{
    future::{self, Either},
    FutureExt as _, TryFutureExt as _,
};

#[cfg(feature = "std")]
use crate::sleep::sleep_until;
use crate::{sleep::sleep, Sleepble};

//
pub fn internal_timeout<SLEEP, T>(
    dur: Duration,
    future: T,
) -> impl Future<Output = Result<T::Output, (Duration, T)>>
where
    SLEEP: Sleepble,
    T: Future + Unpin,
{
    future::select(future, Box::pin(sleep::<SLEEP>(dur))).map(move |either| match either {
        Either::Left((output, _)) => Ok(output),
        Either::Right((_, future)) => Err((dur, future)),
    })
}

pub fn timeout<SLEEP, T>(dur: Duration, future: T) -> impl Future<Output = Result<T::Output, Error>>
where
    SLEEP: Sleepble,
    T: Future + Unpin,
{
    internal_timeout::<SLEEP, _>(dur, future).map_err(|(dur, _)| Error::Timeout(dur))
}

#[cfg(feature = "std")]
pub fn internal_timeout_at<SLEEP, T>(
    deadline: std::time::Instant,
    future: T,
) -> impl Future<Output = Result<T::Output, (std::time::Instant, T)>>
where
    SLEEP: Sleepble,
    T: Future + Unpin,
{
    future::select(future, Box::pin(sleep_until::<SLEEP>(deadline))).map(move |either| match either
    {
        Either::Left((output, _)) => Ok(output),
        Either::Right((_, future)) => Err((deadline, future)),
    })
}

#[cfg(feature = "std")]
pub fn timeout_at<SLEEP, T>(
    deadline: std::time::Instant,
    future: T,
) -> impl Future<Output = Result<T::Output, Error>>
where
    SLEEP: Sleepble,
    T: Future + Unpin,
{
    internal_timeout_at::<SLEEP, _>(deadline, future)
        .map_err(|(instant, _)| Error::TimeoutAt(instant))
}

//
#[derive(Debug, PartialEq)]
pub enum Error {
    Timeout(Duration),
    #[cfg(feature = "std")]
    TimeoutAt(std::time::Instant),
}
impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{self:?}")
    }
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}

#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
    fn from(_err: Error) -> std::io::Error {
        std::io::ErrorKind::TimedOut.into()
    }
}

#[cfg(feature = "impl_tokio")]
#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;

    #[allow(dead_code)]
    async fn foo() -> usize {
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        0
    }

    #[cfg(feature = "std")]
    #[tokio::test]
    async fn test_timeout() {
        //
        #[cfg(feature = "std")]
        let now = std::time::Instant::now();

        let (_tx, rx) = tokio::sync::oneshot::channel::<()>();
        match timeout::<crate::impl_tokio::Sleep, _>(Duration::from_millis(50), rx).await {
            Ok(v) => panic!("{v:?}"),
            Err(err) => assert_eq!(err, Error::Timeout(Duration::from_millis(50))),
        }

        #[cfg(feature = "std")]
        {
            let elapsed_dur = now.elapsed();
            assert!(elapsed_dur.as_millis() >= 50 && elapsed_dur.as_millis() <= 55);
        }

        //
        #[cfg(feature = "std")]
        let now = std::time::Instant::now();

        match timeout::<crate::impl_tokio::Sleep, _>(Duration::from_millis(50), Box::pin(foo()))
            .await
        {
            Ok(v) => panic!("{v:?}"),
            Err(err) => assert_eq!(err, Error::Timeout(Duration::from_millis(50))),
        }

        #[cfg(feature = "std")]
        {
            let elapsed_dur = now.elapsed();
            assert!(elapsed_dur.as_millis() >= 50 && elapsed_dur.as_millis() <= 55);
        }

        //
        #[cfg(feature = "std")]
        let now = std::time::Instant::now();

        match timeout::<crate::impl_tokio::Sleep, _>(Duration::from_millis(150), Box::pin(foo()))
            .await
        {
            Ok(v) => assert_eq!(v, 0),
            Err(err) => panic!("{err:?}"),
        }

        #[cfg(feature = "std")]
        {
            let elapsed_dur = now.elapsed();
            assert!(elapsed_dur.as_millis() >= 100 && elapsed_dur.as_millis() <= 105);
        }
    }

    #[cfg(feature = "std")]
    #[tokio::test]
    async fn test_timeout_at() {
        //
        let now = std::time::Instant::now();

        match timeout_at::<crate::impl_tokio::Sleep, _>(
            std::time::Instant::now() + Duration::from_millis(50),
            Box::pin(foo()),
        )
        .await
        {
            Ok(v) => panic!("{v:?}"),
            Err(Error::Timeout(dur)) => panic!("{dur:?}"),
            Err(Error::TimeoutAt(instant)) => {
                let elapsed_dur = instant.elapsed();
                assert!(elapsed_dur.as_millis() <= 5);
            }
        }

        let elapsed_dur = now.elapsed();
        assert!(elapsed_dur.as_millis() >= 50 && elapsed_dur.as_millis() <= 55);
    }
}