Skip to main content

binary_options_tools_core_pre/utils/
time.rs

1use std::time::Duration;
2
3use core::future::Future;
4
5use crate::error::{CoreError, CoreResult};
6
7pub async fn timeout<F, T, E>(duration: Duration, future: F, task: String) -> CoreResult<T>
8where
9    E: Into<CoreError>,
10    F: Future<Output = Result<T, E>>,
11{
12    let res = tokio::select! {
13        _ = tokio::time::sleep(duration) => Err(CoreError::TimeoutError { task, duration }),
14        result = future => match result {
15            Ok(value) => Ok(value),
16            Err(err) => Err(err.into()),
17        },
18    };
19    res
20}