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§
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.
Provided Methods§
Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Implementors§
impl<T: 'static> Task<T> for TokioLocalTask<T>
Available on crate feature
tokio only.impl<T: Send + 'static> Task<T> for TokioTask<T>
Available on crate feature
tokio only.impl<T: Send> Task<T> for AnyExecutorTask<T>
impl<T> Task<T> for AnyLocalExecutorTask<T>
impl<T> Task<T> for AsyncTask<T>
Available on crate feature
async-task only.