Repository: bevyengine/bevy
File: crates/bevy_tasks/src/task.rs (Rust)

Code before:
impl<T> Task<T> {
    /// Detaches the task to let it keep running in the background.
    ///
    /// When building for the web, this method has no effect.
    pub fn detach(self) {
        cfg::web! {
            if {
                // Tasks are already treated as detached on the web.
            } else {
                self.0.detach();
            }
        }
    }

    /// Cancels the task and waits for it to stop running.
    ///
    /// Returns the task's output if it was completed just before it got canceled, or [`None`] if
    /// it didn't complete.
    ///
    /// While it's possible to simply drop the [`Task`] to cancel it, this is a cleaner way of
    /// canceling because it also waits for the task to stop running.
    ///
    /// Canceling tasks is unsupported on the web, and this is the same as awaiting the task.
    pub async fn cancel(self) -> Option<T> {
        cfg::web! {
            if {
                // Await the task and handle any panics.
                match self.0.recv().await {
                    Ok(Ok(value)) => Some(value),
                    Err(_) => None,
                    Ok(Err(panic)) => {
                        // drop this to prevent the panic payload from resuming the panic on drop.
                        // this also leaks the box but I'm not sure how to avoid that
                        core::mem::forget(panic);
                        None
                    }
                }
            } else {
                // Wait for the task to become canceled
                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 {
        cfg::web! {
            if {

Review comment (suggestion):
```suggestion
    /// # Platform-Specific Behavior
    /// Canceling tasks is unsupported on the web, and is the same as awaiting the task.
```

Code after:
    /// Detaches the task to let it keep running in the background.
    ///
    /// # Platform-Specific Behavior
    ///
    /// When building for the web, this method has no effect.
    pub fn detach(self) {
        cfg::web! {
            if {
                // Tasks are already treated as detached on the web.
            } else {
                self.0.detach();
            }
        }
    }

    /// Cancels the task and waits for it to stop running.
    ///
    /// Returns the task's output if it was completed just before it got canceled, or [`None`] if
    /// it didn't complete.
    ///
    /// While it's possible to simply drop the [`Task`] to cancel it, this is a cleaner way of
    /// canceling because it also waits for the task to stop running.
    ///
    /// # Platform-Specific Behavior
    ///
    /// Canceling tasks is unsupported on the web, and this is the same as awaiting the task.
    pub async fn cancel(self) -> Option<T> {
        cfg::web! {
            if {
                // Await the task and handle any panics.
                match self.0.recv().await {
                    Ok(Ok(value)) => Some(value),
                    Err(_) => None,
                    Ok(Err(panic)) => {
                        // drop this to prevent the panic payload from resuming the panic on drop.
                        // this also leaks the box but I'm not sure how to avoid that
                        core::mem::forget(panic);
                        None
                    }
                }
            } else {
                // Wait for the task to become canceled
                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.