scuffle_future_ext/
lib.rs

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
/// The [`FutureExt`] trait is a trait that provides a more ergonomic way to
/// extend futures with additional functionality. Similar to the `IteratorExt`
/// trait from the `itertools` crate, but for futures.
pub trait FutureExt {
    /// Attach a timeout to the future.
    ///
    /// This is a convenience method that wraps the [`tokio::time::timeout`]
    /// function. The future will automatically cancel after the timeout has
    /// elapsed. This is equivalent to calling
    /// `with_timeout_at(tokio::time::Instant::now() + duration)`.
    fn with_timeout(self, duration: tokio::time::Duration) -> tokio::time::Timeout<Self>
    where
        Self: Sized;

    /// Attach a timeout to the future.
    ///
    /// This is a convenience method that wraps the [`tokio::time::timeout_at`]
    /// function. The future will automatically cancel after the timeout has
    /// elapsed. Unlike the `with_timeout` method, this method allows you to
    /// specify a deadline instead of a duration.
    fn with_timeout_at(self, deadline: tokio::time::Instant) -> tokio::time::Timeout<Self>
    where
        Self: Sized;
}

impl<F: std::future::Future> FutureExt for F {
    fn with_timeout(self, duration: tokio::time::Duration) -> tokio::time::Timeout<Self> {
        tokio::time::timeout(duration, self)
    }

    fn with_timeout_at(self, deadline: tokio::time::Instant) -> tokio::time::Timeout<Self> {
        tokio::time::timeout_at(deadline, self)
    }
}