-
Notifications
You must be signed in to change notification settings - Fork 17
feat: move Clock::timed method to tick #336
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! This example demonstrates how to measure the execution time of an async | ||
| //! operation using [`Clock::timed`] and [`TimedResult`]. | ||
|
|
||
| use tick::{Clock, TimedResult}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| // Create a clock for the Tokio runtime. | ||
| let clock = Clock::new_tokio(); | ||
|
|
||
| // Start some background work that returns a result after a delay. | ||
| let background_job = async { | ||
| clock.delay(std::time::Duration::from_millis(10)).await; | ||
| "Background job result" | ||
| }; | ||
|
|
||
| // Use `Timed` to measure the time taken by the background job and capture its result. | ||
| let TimedResult { result, duration } = clock.timed(background_job).await; | ||
|
|
||
| // Print the result and the elapsed time. | ||
| println!("Result: {}, Elapsed time: {:?}", result, duration); | ||
|
Check failure on line 24 in crates/tick/examples/timed_result.rs
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ use std::time::{Duration, Instant, SystemTime}; | |||||||
| use thread_aware::ThreadAware; | ||||||||
| use thread_aware::affinity::{MemoryAffinity, PinnedAffinity}; | ||||||||
|
|
||||||||
| use crate::Timed; | ||||||||
| use crate::state::ClockState; | ||||||||
| use crate::timers::TimerKey; | ||||||||
|
|
||||||||
|
|
@@ -459,6 +460,35 @@ impl Clock { | |||||||
| crate::Stopwatch::new(self) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Wraps a future so that its execution time is measured. | ||||||||
| /// | ||||||||
| /// Returns a [`Timed`] future whose output is a [`TimedResult`][crate::TimedResult] | ||||||||
| /// containing both the inner future's result and the elapsed duration. | ||||||||
| /// | ||||||||
| /// The measurement uses the same clock as the [`Stopwatch`][crate::Stopwatch], | ||||||||
| /// so time can be controlled in tests via [`ClockControl`][crate::ClockControl]. | ||||||||
| /// | ||||||||
| /// # Examples | ||||||||
| /// | ||||||||
| /// ``` | ||||||||
| /// use tick::{Clock, TimedResult}; | ||||||||
| /// | ||||||||
| /// # async fn timed_example(clock: &Clock) { | ||||||||
| /// let TimedResult { result, duration } = clock.timed(async { 42 }).await; | ||||||||
| /// println!("Result: {}, Duration: {:?}", result, duration); | ||||||||
| /// assert_eq!(result, 42); | ||||||||
|
||||||||
| /// assert_eq!(result, 42); | |
| /// assert_eq!(result, 42); | |
| /// assert!(duration >= Duration::from_millis(0)); |
Copilot
AI
Mar 24, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clock::timed uses F: Future, but Future isn’t in scope in this module (no use std::future::Future; and it’s not in the prelude). This will not compile as-is; import std::future::Future or fully qualify the bound.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This statement is formatted as a single very long line. Please run rustfmt (or break the call across lines) to match the project's formatting expectations and avoid rustfmt CI failures.