diff options
| author | hozan23 <hozan23@proton.me> | 2023-11-08 13:03:27 +0300 | 
|---|---|---|
| committer | hozan23 <hozan23@proton.me> | 2023-11-08 13:03:27 +0300 | 
| commit | 4fe665fc8bc6265baf5bfba6b6a5f3ee2dba63dc (patch) | |
| tree | 77c7c40c9725539546e53b00f424deafe5ec81a8 /karyons_core/src/async_utils/timeout.rs | |
first commit
Diffstat (limited to 'karyons_core/src/async_utils/timeout.rs')
| -rw-r--r-- | karyons_core/src/async_utils/timeout.rs | 52 | 
1 files changed, 52 insertions, 0 deletions
diff --git a/karyons_core/src/async_utils/timeout.rs b/karyons_core/src/async_utils/timeout.rs new file mode 100644 index 0000000..7c55e1b --- /dev/null +++ b/karyons_core/src/async_utils/timeout.rs @@ -0,0 +1,52 @@ +use std::{future::Future, time::Duration}; + +use smol::Timer; + +use super::{select, Either}; +use crate::{error::Error, Result}; + +/// Waits for a future to complete or times out if it exceeds a specified +/// duration. +/// +/// # Example +/// +/// ``` +/// use std::{future, time::Duration}; +/// +/// use karyons_core::async_utils::timeout; +/// +/// async { +///     let fut = future::pending::<()>(); +///     assert!(timeout(Duration::from_millis(100), fut).await.is_err()); +/// }; +/// +/// ``` +/// +pub async fn timeout<T, F>(delay: Duration, future1: F) -> Result<T> +where +    F: Future<Output = T>, +{ +    let result = select(Timer::after(delay), future1).await; + +    match result { +        Either::Left(_) => Err(Error::Timeout), +        Either::Right(res) => Ok(res), +    } +} + +#[cfg(test)] +mod tests { +    use super::*; +    use std::{future, time::Duration}; + +    #[test] +    fn test_timeout() { +        smol::block_on(async move { +            let fut = future::pending::<()>(); +            assert!(timeout(Duration::from_millis(10), fut).await.is_err()); + +            let fut = smol::Timer::after(Duration::from_millis(10)); +            assert!(timeout(Duration::from_millis(50), fut).await.is_ok()) +        }); +    } +}  | 
