TaskHandle

Struct TaskHandle 

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

A task handle that allows to get task result when the task is completed or cancel it if necessary.

Implementations§

Source§

impl<T> TaskHandle<T>

Source

pub fn abort(&self)

Aborts the task associated with the handle.

Source

pub fn cancel(&mut self)

Cancels the task associated with the handle. Whether the task handles cancellation event (using crate::try_await macro) or not depends on task itself. If the task doesn’t implement cancellation handling it continue to execute until it finishes or being aborted.

Examples found in repository?
examples/cancelation.rs (line 31)
19async fn main() {
20    let mut task_handles = Vec::new();
21    for i in 0..5 {
22        let duration = i as u64;
23        task_handles.push(cm::spawn(outer_func(i, duration)));
24    }
25
26    let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
27    for (i, mut handle) in task_handles.into_iter().enumerate() {
28        if tokio::time::timeout_at(deadline, &mut handle).await.is_ok() {
29            println!("task-{} completed", i);
30        } else {
31            handle.cancel();
32            match handle.await {
33                Result::Err(TaskError::Canceled) => println!("task-{} canceled", i),
34                Result::Err(TaskError::Aborted) => println!("task-{} aborted", i),
35                Result::Err(TaskError::Panicked(_)) => println!("task-{} panicked", i),
36                Result::Ok(_) => unreachable!(),
37            }
38        }
39    }
40}

Trait Implementations§

Source§

impl<T> Future for TaskHandle<T>

Source§

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

Polls for the task completion. The task could be completed successfully returning Ok or exited with an Err<TaskError> error. In the last case the actual reason (cancellation, abortion or panicking) could be fetched from the error.

Source§

type Output = Result<T, TaskError>

The type of value produced on completion.

Auto Trait Implementations§

§

impl<T> Freeze for TaskHandle<T>

§

impl<T> !RefUnwindSafe for TaskHandle<T>

§

impl<T> Send for TaskHandle<T>
where T: Send,

§

impl<T> Sync for TaskHandle<T>
where T: Send,

§

impl<T> Unpin for TaskHandle<T>

§

impl<T> !UnwindSafe for TaskHandle<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
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.