pub struct RobustHandle<T> { /* private fields */ }coroutine only.Expand description
Join handle for async task, like Handle, with a more robust mechanism for aborting.
This is more robust than Handle::abort, that aborting
a completed task is possible as long as it’s not joined yet
(even if join has already been called)
Implementations§
Source§impl<T> RobustHandle<T>
impl<T> RobustHandle<T>
Sourcepub fn abort(&self) -> bool
pub fn abort(&self) -> bool
Abort the RobustHandle pointed to by this handle.
Returns if the abort was successful. If false is returned,
that means the task has already been joined.
See documentation above for more detail
Sourcepub fn abort_handle(&self) -> RobustAbortHandle
pub fn abort_handle(&self) -> RobustAbortHandle
Return a handle to remotely and robustly abort the task
Sourcepub fn join(self) -> Result<T>
pub fn join(self) -> Result<T>
Block the current thread to join the task
Panics are caught by the runtime, and will be returned as an Err.
Will error if the task was aborted or panicked.
If you want to handle the abort, use join_maybe_aborted
§Blocking
Do not use this in an async context, since it will block the runtime,
and will panic if the thread is currently driving IO.
Use co_join().await instead.
Sourcepub async fn co_join(self) -> Result<T>
pub async fn co_join(self) -> Result<T>
Wait for the task asynchronously
Panics are caught by the runtime, and will be returned as an Err.
Will error if the task was aborted or panicked.
If you want to handle the abort, use co_join_maybe_aborted
Sourcepub fn join_maybe_aborted(self) -> Result<Option<T>>
pub fn join_maybe_aborted(self) -> Result<Option<T>>
Like join, but returns None if the task was aborted.
Panics are caught by the runtime, and will be returned as an Err.
The task may still be completed with a value if it was aborted.
If you want to access it, see join_maybe_aborted_robust
§Blocking
Do not use this in an async context, since it will block the runtime,
and will panic if the thread is currently driving IO.
Use co_join_maybe_aborted().await instead.
Sourcepub async fn co_join_maybe_aborted(self) -> Result<Option<T>>
pub async fn co_join_maybe_aborted(self) -> Result<Option<T>>
Like join, but returns None if the task was aborted.
Panics are caught by the runtime, and will be returned as an Err.
The task may still be completed with a value if it was aborted.
If you want to access it, see co_join_maybe_aborted_robust
Sourcepub fn join_maybe_aborted_robust(self) -> Result<Result<T, Option<T>>>
pub fn join_maybe_aborted_robust(self) -> Result<Result<T, Option<T>>>
Block the current thread to join the task
Panics are caught by the runtime, and will be returned as an Err.
§Return Value
The outer Result checks if joining is successful, and the inner
indicates if the task was aborted. If the task was completed before
it was aborted, the value is also returned.
- Returns
Ok(Ok(T))if join is successful and task is not aborted - Returns
Ok(Err(None))if join is successful, and task is aborted without finishing - Returns
Ok(Err(Some(T)))if join is successful, and task is aborted, but it was finished anyway. - Returns
Errif join fails.
use std::time::Duration;
let handle = cu::co::spawn(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
42
}).into_robust();
std::thread::sleep(Duration::from_millis(200));
assert!(handle.abort(), "task is not joined yet, so abort is possible");
match handle.join_maybe_aborted_robust() {
Err(e) => panic!("join failed: {e}"),
Ok(Ok(x)) => {
assert!(false, "abort was called, so it can't return Ok(Ok)")
}
Ok(Err(Some(x))) => {
assert_eq!(x, 42, "abort was called after task has already produced value 42");
}
Ok(Err(None)) => {
assert!(false, "abort was not called when the task was still running ")
}
}§Blocking
Use co_join_maybe_aborted_robust().await instead.