pub struct TaskContext { /* private fields */ }Expand description
The context for a task, which can be used to cancel the task or spawn sub-tasks.
Unlike WorkerContext, which is shared across all tasks,
a TaskContext is unique to a single task and can be cloned to share with sub-tasks.
A task context is considered “executed” when the task has successfully completed or has been cancelled, and all sub-tasks have also completed.
Implementations§
Source§impl TaskContext
impl TaskContext
Sourcepub fn cancel(&self) -> Result<(), TaskStateError>
pub fn cancel(&self) -> Result<(), TaskStateError>
Requests cancellation of the task.
Wakes any pending WaitForExecutionFuture futures waiting for execution to complete
§Errors
Returns an error if the task had already reached a terminal state (already cancelled or already completed).
Sourcepub fn is_completed(&self) -> bool
pub fn is_completed(&self) -> bool
Returns whether the task has been completed.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns whether cancellation has been requested.
Sourcepub fn is_executed(&self) -> bool
pub fn is_executed(&self) -> bool
Returns whether there is nothing left to wait on:
If true the task has either completed or been cancelled.
Sourcepub fn executed(&self) -> WaitForExecutionFuture ⓘ
pub fn executed(&self) -> WaitForExecutionFuture ⓘ
Returns a future that resolves once the task is executed — either completed or cancelled.
Can be awaited standalone or raced against other futures, e.g.:
futures_util::select! {
_ = ctx.executed().fuse() => { /* task done */ }
res = some_work.fuse() => { /* completed with `res` */ }
}Sourcepub fn execution_context(&self) -> Option<Arc<ExecutionContext>>
pub fn execution_context(&self) -> Option<Arc<ExecutionContext>>
Recovers the execution context for the task, if it’s still available.
Returns None once the owning ExecutionContext has been dropped.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of sub-tasks currently spawned by this task context.
Sourcepub fn run_until_executed<F>(&self, fut: F) -> SubTaskFuture<F> ⓘwhere
F: Future,
pub fn run_until_executed<F>(&self, fut: F) -> SubTaskFuture<F> ⓘwhere
F: Future,
Spawns a (sub-task) future that is tied to this task context.
Runs a future to completion, returning its result unless this
TaskContext is completed first.
Biased towards completion: if the future resolves and the task is completed in the same poll, the future’s result wins.
Trait Implementations§
Source§impl Borrow<str> for TaskContext
impl Borrow<str> for TaskContext
Source§impl Clone for TaskContext
impl Clone for TaskContext
Source§fn clone(&self) -> TaskContext
fn clone(&self) -> TaskContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more