-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I find the syntax for `tokio::time::timeout` to be quite ugly, since it breaks the typical chained method calls. With the FutureExt trait we can just call `.with_timeout(..)` instead which seems to be a cleaner way to do it.
- Loading branch information
1 parent
e4800b4
commit 8b5bc9b
Showing
14 changed files
with
106 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "scuffle-future-ext" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
||
[dependencies] | ||
tokio = { version = "1", features = ["time"] } | ||
scuffle-workspace-hack.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/// The [`FutureExt`] trait is a trait that provides a more ergonomic way to | ||
/// extend futures with additional functionality. Similar to the [`IteratorExt`] | ||
/// trait, but for futures. | ||
pub trait FutureExt { | ||
/// Attach a timeout to the future. | ||
/// | ||
/// The timeout is relative to the current time. | ||
fn with_timeout(self, duration: tokio::time::Duration) -> tokio::time::Timeout<Self> | ||
where | ||
Self: Sized; | ||
|
||
/// Similar to `with_timeout`, but the timeout is absolute. | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters