Skip to content

Commit efcd9db

Browse files
lgxbslgxYoung-Flash
authored andcommitted
Add async methods to module trace (#2)
* Add async methods. * Format.
1 parent 43c2e29 commit efcd9db

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/trace.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Chrome trace output.
22
33
use std::fs::File;
4+
use std::future::Future;
45
use std::io::{BufWriter, Write};
56
use std::time::Instant;
67

@@ -56,6 +57,14 @@ impl Trace {
5657
result
5758
}
5859

60+
async fn async_scope<T>(&mut self, name: &str, f: impl Future<Output = T>) -> T {
61+
let start = Instant::now();
62+
let result = f.await;
63+
let end = Instant::now();
64+
self.write_complete(name, 0, start, end);
65+
result
66+
}
67+
5968
/*
6069
These functions were useful when developing, but are currently unused.
6170
@@ -121,6 +130,18 @@ pub fn scope<T>(name: &'static str, f: impl FnOnce() -> T) -> T {
121130
}
122131
}
123132

133+
#[inline]
134+
#[allow(static_mut_refs)]
135+
pub async fn async_scope<T>(name: &'static str, f: impl Future<Output = T>) -> T {
136+
// Safety: accessing global mut, not threadsafe.
137+
unsafe {
138+
match &mut TRACE {
139+
None => f.await,
140+
Some(t) => t.async_scope(name, f).await,
141+
}
142+
}
143+
}
144+
124145
pub fn close() {
125146
if_enabled(|t| t.close());
126147
}

0 commit comments

Comments
 (0)