Struct Task

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

A task reference that runs its future.

At any moment in time, there is at most one Task reference associated with a particular task. Running consumes the Task reference and polls its internal future. If the future is still pending after getting polled, the Task reference simply won’t exist until a Waker notifies the task. If the future completes, its result becomes available to the JoinHandle.

When a task is woken up, its Task reference is recreated and passed to the schedule function. In most executors, scheduling simply pushes the Task reference into a queue of runnable tasks.

If the Task reference is dropped without getting run, the task is automatically canceled. When canceled, the task won’t be scheduled again even if a Waker wakes it. It is possible for the JoinHandle to cancel while the Task reference exists, in which case an attempt to run the task won’t do anything.

Implementations§

Source§

impl<T> Task<T>

Source

pub fn schedule(self)

Schedules the task.

This is a convenience method that simply reschedules the task by passing it to its schedule function.

If the task is canceled, this method won’t do anything.

Source

pub fn run(self) -> bool

Runs the task.

Returns true if the task was woken while running, in which case it gets rescheduled at the end of this method invocation.

This method polls the task’s future. If the future completes, its result will become available to the JoinHandle. And if the future is still pending, the task will have to be woken up in order to be rescheduled and run again.

If the task was canceled by a JoinHandle before it gets run, then this method won’t do anything.

It is possible that polling the future panics, in which case the panic will be propagated into the caller. It is advised that invocations of this method are wrapped inside catch_unwind. If a panic occurs, the task is automatically canceled.

Source

pub fn cancel(&self)

Cancels the task.

When canceled, the task won’t be scheduled again even if a Waker wakes it. An attempt to run it won’t do anything.

Source

pub fn tag(&self) -> &T

Returns a reference to the tag stored inside the task.

Source

pub fn into_raw(self) -> *const T

Converts this task into a raw pointer to the tag.

Source

pub unsafe fn from_raw(raw: *const T) -> Task<T>

Converts a raw pointer to the tag into a task.

This method should only be used with raw pointers returned from into_raw.

Source

pub fn waker(&self) -> Waker

Returns a waker associated with this task.

Trait Implementations§

Source§

impl<T: Debug> 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§

impl<T> Send for Task<T>

Source§

impl<T> Sync for Task<T>

Auto Trait Implementations§

§

impl<T> Freeze for Task<T>

§

impl<T> RefUnwindSafe for Task<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Task<T>
where T: Unpin,

§

impl<T> UnwindSafe for Task<T>
where T: UnwindSafe,

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