try_await

Macro try_await 

Source
macro_rules! try_await {
    ($fut: expr) => { ... };
}
Expand description

Similar to await keywords but support cancellation. If current task has been canceled try_await! returns Canceled otherwise it waits for the future readiness and returns Completed where T is the result of the future.

NOTE: The future must be cancellation safe.

Basic usage:

use coachman::{try_await, Canceled, Completed};

async fn cancelable_func() {
    match try_await!(tokio::time::sleep(std::time::Duration::from_secs(5))) {
        Canceled => println!("task canceled"),
        Completed(_) => println!("sleep completed"),
    };
}