Skip to main content

geph5_rt/
timeout.rs

1//! A `.timeout()` future combinator backed by `tokio::time`, preserving the
2//! `smol-timeout2` contract (returns `None` on timeout) so existing call sites
3//! need only swap the import.
4
5use std::future::Future;
6use std::time::Duration;
7
8/// Extension trait adding a `.timeout(duration)` combinator to any future.
9///
10/// Returns `Some(output)` if the future completes in time, or `None` if it
11/// elapses — matching `smol_timeout2::TimeoutExt`.
12pub trait TimeoutExt: Future + Sized {
13    fn timeout(self, duration: Duration) -> impl Future<Output = Option<Self::Output>> {
14        async move { tokio::time::timeout(duration, self).await.ok() }
15    }
16}
17
18impl<F: Future> TimeoutExt for F {}