pub struct Timeout { /* private fields */ }
Expand description
See more information about reset
and stop
methods
Implementations§
Source§impl Timeout
impl Timeout
Sourcepub async fn set<T>(
timeout: Duration,
f: impl Future<Output = T> + Send + 'static,
) -> Self
pub async fn set<T>( timeout: Duration, f: impl Future<Output = T> + Send + 'static, ) -> Self
Schedule the task inside closure after timeout
Sourcepub async fn reset(&self, timeout: Duration)
pub async fn reset(&self, timeout: Duration)
Reset timeout with a new value, i.e. to delay execution of your task
§Example
use std::time::Duration;
use async_timeouts::Timeout;
use tokio::sync::Notify;
use std::sync::Arc;
#[tokio::main]
async fn main() {
let event = Arc::new(Notify::new());
let event_cloned = event.clone();
let delayed_task = Timeout::set(Duration::from_secs(3), async move {
println!("This message will be printed after 6 seconds");
event_cloned.notify_one();
}).await;
delayed_task.reset(Duration::from_secs(6)).await;
event.notified().await;
}
Sourcepub async fn restart<T>(
&mut self,
timeout: Duration,
f: impl Future<Output = T> + Send + 'static,
)
pub async fn restart<T>( &mut self, timeout: Duration, f: impl Future<Output = T> + Send + 'static, )
Restart timeout with a new or previous task. Timeout
instance will be reused if the
previous timer is over, or the previous task will be stoped ahead of time if not.
§Example
use std::time::{Duration, Instant};
use async_timeouts::Timeout;
use async_channel::unbounded;
#[tokio::main]
async fn main() {
let timer = Instant::now();
let (task_tx, task_rx) = unbounded::<i32>();
let mut delayed_task = {
let task_tx = task_tx.clone();
Timeout::set(Duration::from_secs(3), async move {
let _ = task_tx.send(1).await;
}).await
};
delayed_task.restart(Duration::from_secs(6), async move {
let _ = task_tx.send(2).await;
}).await;
if let Ok(msg) = task_rx.recv().await {
println!("Task is finished {msg}");
}
assert!(timer.elapsed().as_secs() >= 6);
}
Sourcepub async fn stop(&self)
pub async fn stop(&self)
Stop the timer before your task will be executed
§Example
use std::time::Duration;
use async_timeouts::Timeout;
use tokio::sync::Notify;
use std::sync::Arc;
#[tokio::main]
async fn main() {
let event = Arc::new(Notify::new());
let event_cloned = event.clone();
let delayed_task = Timeout::set(Duration::from_secs(3), async move {
println!("This message will never be printed and event will never be notified.");
event_cloned.notify_one();
}).await;
delayed_task.stop().await;
event.notified().await;
}
Sourcepub fn finished(&self) -> bool
pub fn finished(&self) -> bool
Check if the timer of the task is over or not
§Example
use std::time::Duration;
use async_timeouts::Timeout;
use tokio::sync::Notify;
use std::sync::Arc;
#[tokio::main]
async fn main() {
let event = Arc::new(Notify::new());
let event_cloned = event.clone();
let delayed_task = Timeout::set(Duration::from_secs(3), async move {
event_cloned.notify_one();
}).await;
assert!(!delayed_task.finished());
println!("delayed_task is not finished yet: {}", delayed_task.finished());
event.notified().await;
assert!(delayed_task.finished());
println!("delayed_task is finished: {}", delayed_task.finished());
}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Timeout
impl RefUnwindSafe for Timeout
impl Send for Timeout
impl Sync for Timeout
impl Unpin for Timeout
impl UnwindSafe for Timeout
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more