async_timeouts/
lib.rs

1// Copyright 2024 Artyom Sinyugin
2
3//! # Async Timeouts Helper
4//! It is a simple instrument to delay execution of an async task with additional methods:
5//! * [reset] timeout with a new value, i.e. to delay execution of your task;
6//! * [restart] timeout with a new or previous task;
7//! * [stop] the timer before your task will be executed;
8//! * [finished] — check if the timer of the task is over or not.
9//! 
10//! It is convinient to use this crate with [Notify] or different 
11//! channels (i.e. [async_channel]).
12//! 
13//! ## Example 
14//! ```rust
15//! use std::time::{Duration, Instant};
16//! use async_timeouts::Timeout;
17//! use tokio::sync::Notify;
18//! use std::sync::Arc;
19//! 
20//! #[tokio::main]
21//! async fn main() {
22//!     let event = Arc::new(Notify::new());
23//!     let timer = Instant::now();
24//!     {
25//!         let event = event.clone();
26//!         // Let's notify our event after 3 seconds
27//!         Timeout::set(Duration::from_secs(3), async move {
28//!             event.notify_one();
29//!         }).await;
30//!     }
31//!     event.notified().await;
32//!     println!("{} seconds elapsed", timer.elapsed().as_secs());
33//! }
34//! ```
35//! [async_channel]: https://crates.io/crates/async-channel
36//! [Notify]: https://docs.rs/tokio/1.44.2/tokio/sync/struct.Notify.html
37//! [reset]: https://docs.rs/async-timeouts/0.1.1/async_timeouts/struct.Timeout.html#method.reset
38//! [restart]: https://docs.rs/async-timeouts/0.1.1/async_timeouts/struct.Timeout.html#method.restart
39//! [stop]: https://docs.rs/async-timeouts/0.1.1/async_timeouts/struct.Timeout.html#method.stop
40//! [finished]: https://docs.rs/async-timeouts/0.1.1/async_timeouts/struct.Timeout.html#method.finished
41mod timeout;
42
43pub use timeout::Timeout;