Skip to main content

RobustHandle

Struct RobustHandle 

Source
pub struct RobustHandle<T> { /* private fields */ }
Available on crate feature 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>

Source

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

Source

pub fn abort_handle(&self) -> RobustAbortHandle

Return a handle to remotely and robustly abort the task

Source

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.

Source

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

Source

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.

Source

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

Source

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

Source

pub async fn co_join_maybe_aborted_robust(self) -> Result<Result<T, Option<T>>>

Wait for the task asynchronously

Panics are caught by the runtime, and will be returned as an Err.

§Return Value

See join_maybe_aborted_robust

Trait Implementations§

Source§

impl<T> From<Handle<T>> for RobustHandle<T>

Source§

fn from(value: Handle<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for RobustHandle<T>
where Handle<T>: Freeze,

§

impl<T> RefUnwindSafe for RobustHandle<T>

§

impl<T> Send for RobustHandle<T>
where Handle<T>: Send,

§

impl<T> Sync for RobustHandle<T>
where Handle<T>: Sync,

§

impl<T> Unpin for RobustHandle<T>
where Handle<T>: Unpin,

§

impl<T> UnsafeUnpin for RobustHandle<T>
where Handle<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for RobustHandle<T>
where Handle<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.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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.