Skip to content

Commit

Permalink
Add async methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgxbslgx committed Sep 9, 2024
1 parent 57092cd commit 3624e15
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/trace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Chrome trace output.
use std::fs::File;
use std::future::Future;
use std::io::{BufWriter, Write};
use std::time::Instant;

Expand Down Expand Up @@ -56,6 +57,14 @@ impl Trace {
result
}

async fn async_scope<T>(&mut self, name: &str, f: impl Future<Output = T>) -> T {
let start = Instant::now();
let result = f.await;
let end = Instant::now();
self.write_complete(name, 0, start, end);
result
}

/*
These functions were useful when developing, but are currently unused.
Expand Down Expand Up @@ -121,6 +130,18 @@ pub fn scope<T>(name: &'static str, f: impl FnOnce() -> T) -> T {
}
}

#[inline]
#[allow(static_mut_refs)]
pub async fn async_scope<T>(name: &'static str, f: impl Future<Output = T>) -> T {
// Safety: accessing global mut, not threadsafe.
unsafe {
match &mut TRACE {
None => f.await,
Some(t) => t.async_scope(name, f).await,
}
}
}

pub fn close() {
if_enabled(|t| t.close());
}

0 comments on commit 3624e15

Please sign in to comment.