From d0953e833d1e5bfdafe1293316eaf5dd3bb90569 Mon Sep 17 00:00:00 2001 From: Tudyx <56633664+Tudyx@users.noreply.github.com> Date: Thu, 2 Oct 2025 09:07:28 +0200 Subject: [PATCH] task: simplify the example of `TaskTracker` (#7657) --- tokio-util/src/task/task_tracker.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tokio-util/src/task/task_tracker.rs b/tokio-util/src/task/task_tracker.rs index e168baa42..140e66d21 100644 --- a/tokio-util/src/task/task_tracker.rs +++ b/tokio-util/src/task/task_tracker.rs @@ -92,6 +92,8 @@ use tokio::{ /// ``` /// use tokio_util::sync::CancellationToken; /// use tokio_util::task::TaskTracker; +/// use tokio_util::time::FutureExt; +/// /// use tokio::time::{self, Duration}; /// /// async fn background_task(num: u64) { @@ -111,17 +113,18 @@ use tokio::{ /// for i in 0..10 { /// let token = token.clone(); /// tracker.spawn(async move { -/// // Use a `tokio::select!` to kill the background task if the token is -/// // cancelled. -/// tokio::select! { -/// () = background_task(i) => { -/// println!("Task {} exiting normally.", i); -/// }, -/// () = token.cancelled() => { +/// // Use a `with_cancellation_token_owned` to kill the background task +/// // if the token is cancelled. +/// match background_task(i) +/// .with_cancellation_token_owned(token) +/// .await +/// { +/// Some(()) => println!("Task {} exiting normally.", i), +/// None => { /// // Do some cleanup before we really exit. /// time::sleep(Duration::from_millis(50)).await; /// println!("Task {} finished cleanup.", i); -/// }, +/// } /// } /// }); /// }