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
use derive_more::{Display, Error};
use std::time::{Duration, SystemTime, SystemTimeError};

#[derive(Debug, Display, Error)]
pub enum Error {
    Exec(#[error(ignore)] Box<dyn std::error::Error>),
    SystemTime(SystemTimeError),
    #[display(fmt = "{:?}", _0)]
    Timeout(#[error(ignore)] Duration),
}

/// Invokes a function repeatedly until it yields Some(T); if a timeout is reached,
/// the function will return an error
pub fn loop_timeout<T>(
    timeout: Duration,
    mut f: impl FnMut() -> Result<Option<T>, Box<dyn std::error::Error>>,
) -> Result<T, Error> {
    let start_time = SystemTime::now();
    let mut result = None;
    while SystemTime::now()
        .duration_since(start_time)
        .map_err(Error::SystemTime)?
        < timeout
        && result.is_none()
    {
        result = f().map_err(Error::Exec)?;
    }

    result.ok_or(Error::Timeout(timeout))
}

/// Invokes a function repeatedly until it yields true; if a timeout is
/// reached, the function will panic
pub fn loop_timeout_panic<T>(
    timeout: Duration,
    mut f: impl FnMut() -> Option<T>,
) -> T {
    loop_timeout(timeout, || Ok(f())).unwrap()
}