Skip to main content

TaskContext

Struct TaskContext 

Source
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

Source

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

Source

pub fn is_completed(&self) -> bool

Returns whether the task has been completed.

Source

pub fn is_cancelled(&self) -> bool

Returns whether cancellation has been requested.

Source

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.

Source

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` */ }
}
Source

pub fn elapsed(&self) -> Duration

Returns how long the task has been running.

Source

pub fn task_id(&self) -> &str

Returns the task id for this token.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of sub-tasks currently spawned by this task context.

Source

pub fn is_empty(&self) -> bool

Returns whether there are any subtasks running.

Source

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

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl Clone for TaskContext

Source§

fn clone(&self) -> TaskContext

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TaskContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for TaskContext

Source§

impl<Args: Sync> FromRequest<Task<Args>> for TaskContext

Source§

type Error = MissingDataError

The error type that can occur during extraction.
Source§

async fn from_request(task: &Task<Args>) -> Result<Self, Self::Error>

Perform the extraction.
Source§

impl Hash for TaskContext

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for TaskContext

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more