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>
impl<T> Task<T>
Sourcepub fn schedule(self)
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.
Sourcepub fn run(self) -> bool
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.
Sourcepub fn cancel(&self)
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.