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 detach(self)
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§
Sourcefn poll_result(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<T, Box<dyn Any + Send>>>
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.
Sourcefn poll_cancel(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>
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§
Sourcefn result(self) -> impl Future<Output = Result<T, Box<dyn Any + Send>>>where
Self: Sized,
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.
Implementations on Foreign Types§
Implementors§
impl<T> Task<T> for AsyncTask<T>
Available on crate feature
async-task
only.impl<T> Task<T> for AnyLocalExecutorTask<T>
impl<T: 'static> Task<T> for TokioLocalTask<T>
Available on crate feature
tokio
only.impl<T: 'static> Task<T> for WebTask<T>
Available on crate feature
web
only.impl<T: Send + 'static> Task<T> for TokioTask<T>
Available on crate feature
tokio
only.