async-timeouts 0.3.2

An instrument to start async tasks after timeouts
Documentation
use std::{
    sync::Arc,
    time::{Duration, Instant},
};

use async_channel::unbounded;
use async_timeouts::Timeout;
use tokio::sync::Notify;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    println!("Start!");
    let timer = Instant::now();
    let event = Arc::new(Notify::new());
    let (tx, rx) = unbounded::<u64>();

    let task = {
        let notif = event.clone();
        async move {
            tx.send(6).await.unwrap();
            tokio::time::sleep(Duration::from_secs(3)).await;
            notif.notify_one();
        }
    };

    // Timeout should be expired in 3 seconds and to start our task ;
    let delayed_task = Timeout::set(Duration::from_secs(3), task).await;

    {
        let delayed_task = delayed_task.clone();
        let event = event.clone();
        tokio::spawn(async move {
            // Lets change the timeout in a different tokio task
            let _ = delayed_task.reset(Duration::from_secs(6)).await;
            match rx.recv().await {
                Ok(msg) => println!("Six seconds elapsed. Received from channel: {}", msg),
                Err(_) => {
                    println!("Our delayed task is cancelled earlier than expected via 'x.stop()'. Async channel is closed.");
                    event.notify_one();
                }
            }
        });
    }

    event.notified().await;
    println!(
        "It must be 9 seconds now after the start: {} seconds",
        timer.elapsed().as_secs()
    );
}