Task

Trait Task 

Source
pub trait Task<T>: Future<Output = T> {
    // Required method
    fn poll_result(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Result<T, Box<dyn Any + Send>>>;

    // Provided methods
    fn result(self) -> impl Future<Output = Result<T, Box<dyn Any + Send>>>
       where Self: Sized { ... }
    fn detach(self)
       where Self: Sized { ... }
}
Expand description

A trait representing a spawned task that can be awaited or queried for results.

This trait extends Future with additional capabilities for task management:

  • Explicit error handling via poll_result
  • Convenience methods for getting results and detaching

Dropping a task cancels its execution.

Required Methods§

Source

fn poll_result( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<T, Box<dyn Any + Send>>>

Poll the task for completion, returning a Result that can contain errors.

Unlike the Future::poll implementation, this method allows you to handle task panics and other errors explicitly rather than propagating them.

Provided Methods§

Source

fn result(self) -> impl Future<Output = Result<T, Box<dyn Any + Send>>>
where Self: Sized,

Get the result of the task, including any errors that occurred.

This is equivalent to awaiting the task but returns a Result that allows you to handle panics and other errors explicitly.

Source

fn detach(self)
where Self: Sized,

Detach the task, allowing it to run in the background without being awaited.

Once detached, the task will continue running but its result cannot be retrieved. This is useful for fire-and-forget operations where you don’t need to wait for or handle the result.

Implementations§

Source§

impl<T> dyn Task<T>

Source

pub async fn result(self: Box<Self>) -> Result<T, Box<dyn Any + Send>>

Get the result of the boxed task, including any errors that occurred.

This method awaits the task completion and returns a Result that allows you to handle panics and other errors explicitly.

Implementations on Foreign Types§

Source§

impl<T: Task<T>> Task<T> for Pin<Box<T>>

Source§

fn poll_result( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<T, Box<dyn Any + Send>>>

Implementors§

Source§

impl<T> Task<T> for AsyncTask<T>

Available on crate feature async-task only.
Source§

impl<T> Task<T> for AnyLocalExecutorTask<T>

Source§

impl<T: 'static> Task<T> for TokioLocalTask<T>

Available on crate feature tokio only.
Source§

impl<T: Send + 'static> Task<T> for TokioTask<T>

Available on crate feature tokio only.
Source§

impl<T: Send> Task<T> for AnyExecutorTask<T>