Skip to content

Commit c408be4

Browse files
author
Ariel Ben-Yehuda
committed
address review comments
1 parent 6e64f61 commit c408be4

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

tokio/src/runtime/dump.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,21 @@ impl Trace {
227227
///
228228
/// Example usage:
229229
/// ```
230-
/// let mut trace = None;
231-
///
232230
/// // some future
233-
/// let mut test_future = Box::pin(async move { tokio::task::yield_now().await; 0 });
234-
///
231+
/// let mut test_future = std::pin::pin!(async move { tokio::task::yield_now().await; 0 });
232+
///
235233
/// // trace it once
236-
/// Trace::root(std::future::poll_fn(|cx| {
237-
/// if trace.is_none() {
238-
/// // make sure we only trace once
239-
/// let (res, captured) = Trace::capture(|| test_future.as_mut().poll(cx));
240-
/// trace = Some(captured);
241-
/// res
242-
/// } else {
243-
/// test_future.as_mut().poll(cx)
244-
/// }
245-
/// })).await;@@
234+
/// let (trace, res) = Trace::root(std::future::poll_fn(|cx| {
235+
/// let (res, trace) = Trace::capture(|| test_future.as_mut().poll(cx));
236+
/// Poll::Ready((trace, res))
237+
/// })).await;
238+
///
239+
/// let output = match res {
240+
/// Poll::Ready(output) => output,
241+
/// Poll::Pending => test_future.await,
242+
/// };
246243
///
247-
/// // check that there are backtraces
248-
/// assert!(!trace.unwrap().resolve_backtraces().is_empty());
244+
/// println!("{trace}");
249245
/// ```
250246
///
251247
/// ### Nested calls

tokio/tests/task_trace_self.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@
66
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64")
77
))]
88

9-
use std::{
10-
future::Future,
11-
pin::Pin,
12-
sync::{Arc, Mutex},
13-
task::{Context, Poll},
14-
time::{Duration, Instant},
15-
};
9+
use std::future::Future;
10+
use std::pin::Pin;
11+
use std::sync::{Arc, Mutex};
12+
use std::task::{Context, Poll};
13+
use std::time::{Duration, Instant};
1614

1715
use tokio::runtime::dump::{Root, Trace};
1816

19-
pin_project_lite::pin_project! { pub struct PrettyFuture<F: Future> {
20-
#[pin]
21-
f: Root<F>,
22-
t_last: State,
23-
logs: Arc<Mutex<Vec<Trace>>>,
24-
}
17+
pin_project_lite::pin_project! {
18+
pub struct PrettyFuture<F: Future> {
19+
#[pin]
20+
f: Root<F>,
21+
t_last: State,
22+
logs: Arc<Mutex<Vec<Trace>>>,
23+
}
2524
}
2625

2726
enum State {
@@ -106,3 +105,21 @@ async fn task_trace_self() {
106105
.any(|x| format!("{}", x).contains(&s)));
107106
}
108107
}
108+
109+
async fn test() {
110+
// some future
111+
let mut test_future = std::pin::pin!(async move { tokio::task::yield_now().await; 0 });
112+
113+
// trace it once
114+
let (trace, res) = Trace::root(std::future::poll_fn(|cx| {
115+
let (res, trace) = Trace::capture(|| test_future.as_mut().poll(cx));
116+
Poll::Ready((trace, res))
117+
})).await;
118+
119+
let output = match res {
120+
Poll::Ready(output) => output,
121+
Poll::Pending => test_future.await,
122+
};
123+
124+
println!("{trace}");
125+
}

0 commit comments

Comments
 (0)