turbo_tasks/
duration_span.rs1use std::time::Instant;
2
3pub struct DurationSpanGuard<F: FnOnce(u64)> {
6 start: Instant,
7 f: Option<F>,
8}
9
10impl<F: FnOnce(u64)> DurationSpanGuard<F> {
11 pub fn new(f: F) -> Self {
12 Self {
13 start: Instant::now(),
14 f: Some(f),
15 }
16 }
17}
18
19impl<F: FnOnce(u64)> Drop for DurationSpanGuard<F> {
20 fn drop(&mut self) {
21 if let Some(f) = self.f.take() {
22 f(self.start.elapsed().as_micros() as u64);
23 }
24 }
25}
26
27#[macro_export]
34macro_rules! duration_span {
35 ($name:literal) => {
36 turbo_tasks::duration_span::DurationSpanGuard::new(|duration| {
37 turbo_tasks::macro_helpers::tracing::info!(name = $name, duration = duration);
38 })
39 };
40 ($name:literal, $($arg:tt)+) => {
41 turbo_tasks::duration_span::DurationSpanGuard::new(|duration| {
42 turbo_tasks::macro_helpers::tracing::info!(name = $name, $($arg)+, duration = duration);
43 })
44 };
45}