Skip to main content

Task

Struct Task 

Source
pub struct Task<T> { /* private fields */ }
Expand description

A spawned task.

Awaiting it gives the task’s output. Dropping it cancels the task: its future is dropped without being polled again. detach lets the task run to its end without a handle.

If the task’s future panics, awaiting the handle resumes the panic.

§Panics

Awaiting a task that was cancelled before it completed (through an AbortHandle, or by its runtime’s shutdown) panics; join answers a JoinError instead, and fallible gives a handle that returns None.

Implementations§

Source§

impl<T> Task<T>

Source

pub fn detach(self)

Lets the task run to its end without a handle; its output is dropped.

Source

pub async fn cancel(self) -> Option<T>

Cancels the task and waits for its future to be dropped. Returns the output if the task had already completed.

Source

pub fn fallible(self) -> FallibleTask<T> ⓘ

A handle that returns None where this one panics: when the task was cancelled, or its runtime dropped it before it completed.

Source

pub fn is_finished(&self) -> bool

Whether the task completed or was cancelled. Awaiting a finished task returns at once.

Source

pub fn id(&self) -> Id

The task’s id.

Source

pub fn abort(&self)

Cancels the task without waiting: its future is dropped where it runs, and awaiting the handle then finds no output (unless the task had completed). join reports it as a cancelled JoinError.

Source

pub fn abort_handle(&self) -> AbortHandle

A handle that cancels the task from anywhere, without the Task: it can be cloned and sent to another thread, and outlives the handle.

Source

pub fn join(self) -> Join<T> ⓘ

A future that gives the task’s output, or a JoinError saying why there is none: the task was cancelled (its runtime dropped it too), or its future panicked, with the payload.

Trait Implementations§

Source§

impl<T> Debug for Task<T>

Source§

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

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

impl<T> Drop for Task<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T> Future for Task<T>

Source§

type Output = T

The type of value produced on completion.
Source§

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

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl<T> RefUnwindSafe for Task<T>

Source§

impl<T: Send> Send for Task<T>

Source§

impl<T> Sync for Task<T>

Source§

impl<T> Unpin for Task<T>

Source§

impl<T> UnwindSafe for Task<T>

Auto Trait Implementations§

§

impl<T> Freeze for Task<T>

§

impl<T> UnsafeUnpin for Task<T>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<F, T, E> TryFuture for F
where F: Future<Output = Result<T, E>> + ?Sized,

§

type Ok = T

The type of successful values yielded by this future
§

type Error = E

The type of failures yielded by this future
§

fn try_poll( self: Pin<&mut F>, cx: &mut Context<'_>, ) -> Poll<<F as Future>::Output>

Poll this TryFuture as if it were a Future. Read more
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.