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>
impl<T> TaskHandle<T>
Sourcepub fn cancel(&mut self)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more