Task

Trait Task 

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

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

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

This trait extends Future with additional capabilities for task management:

  • Explicit error handling via poll_result
  • Task cancellation via poll_cancel
  • Convenience methods for getting results and cancelling

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.

Source

fn poll_cancel(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>

Poll for task cancellation.

This method attempts to cancel the task and returns Poll::Ready when the cancellation is complete.

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 cancel(self) -> impl Future<Output = ()>
where Self: Sized,

Cancel the task.

This method requests cancellation of the task and returns a future that completes when the cancellation is finished.

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>>>

Source§

fn poll_cancel(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>

Implementors§

Source§

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

Available on crate feature async-executor 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: 'static> Task<T> for WebTask<T>

Available on crate feature web 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>