Skip to content

Introduce Timed to track how long a future is alive for #7274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

narwhaljs
Copy link

This aims to add a simple feature that allows users to easily track how long a future was alive for. It attempts to follow all standards similar to Timeout. It can be constructed manually through the tokio::time::timed() function, or through the extension trait allowing any_future to call any_future.timed().

Motivation

Are you tired of writing:

let now = std::time::Instant::now();
do_some_async_work().await;
let elapsed = now.elapsed();
println!("Finished some async work in {elapsed:?}");

Solution

Now you can write:

let (_, elapsed) = do_some_async_work().timed().await;
println!("Finished some async work in {elapsed:?}");

@maminrayej maminrayej added A-tokio-util Area: The tokio-util crate A-tokio Area: The main tokio crate M-time Module: tokio/time labels Apr 17, 2025
@mox692
Copy link
Member

mox692 commented Apr 22, 2025

Hmm, I think other crates can provide similar features, so I'm not sure if we should include this in tokio.

@narwhaljs
Copy link
Author

narwhaljs commented Apr 23, 2025

Hmm, I think other crates can provide similar features, so I'm not sure if we should include this in tokio.

Yeah i think futures is probably a better spot for it, or any other ideas? Or is there something that does this already? Fine to close this. It did feel like it slotted decent into the time feature of tokio though but i agree its fairly generic.

@mox692
Copy link
Member

mox692 commented May 3, 2025

I believe it's generally better not to include functionality in tokio that can be implemented in other crates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate A-tokio-util Area: The tokio-util crate M-time Module: tokio/time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants