From a1684a6519062dd548ef49d2928f8ab14fba49e8 Mon Sep 17 00:00:00 2001 From: BeastLe9enD Date: Wed, 2 Nov 2022 12:27:22 +0000 Subject: [PATCH] Add `is_finished` to `Task` (#6444) # Objective In some scenarios it can be useful to check if a task has been finished without polling it. I added a function called `is_finished` to check if a task has been finished. ## Solution Since `async_task` supports it out of the box, it is just a simple wrapper function. --- --- crates/bevy_tasks/src/task.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/bevy_tasks/src/task.rs b/crates/bevy_tasks/src/task.rs index b4f6b1eae717d..360909c2e9671 100644 --- a/crates/bevy_tasks/src/task.rs +++ b/crates/bevy_tasks/src/task.rs @@ -41,6 +41,15 @@ impl Task { pub async fn cancel(self) -> Option { self.0.cancel().await } + + /// Returns `true` if the current task is finished. + /// + /// + /// Unlike poll, it doesn't resolve the final value, it just checks if the task has finished. + /// Note that in a multithreaded environment, this task can be finished immediately after calling this function. + pub fn is_finished(&self) -> bool { + self.0.is_finished() + } } impl Future for Task {