Struct async_shared_timeout::Timeout
source · pub struct Timeout<R: Runtime> { /* private fields */ }
Expand description
A shared timeout.
Example
use std::time::Duration;
let timeout_secs = Duration::from_secs(10);
// Use the tokio runtime
let runtime = async_shared_timeout::runtime::Tokio::new();
let timeout = async_shared_timeout::Timeout::new(runtime, timeout_secs);
tokio::select! {
_ = timeout.wait() => {
println!("timeout expired!");
}
_ = async {
while let Some(cmd) = read_command().await {
println!("command received: {:?}", cmd);
timeout.reset();
}
} => {
println!("no more commands!");
}
}
Implementations§
source§impl<R: Runtime> Timeout<R>
impl<R: Runtime> Timeout<R>
sourcepub fn new(runtime: R, default_timeout: Duration) -> Self
pub fn new(runtime: R, default_timeout: Duration) -> Self
Create a new timeout that expires after default_timeout
Panics
Panics if default_timeout
is longer than ~584 years
sourcepub fn reset(&self)
pub fn reset(&self)
Reset the timeout to the default time.
This function is cheap to call.
Panics
Panics if over ~584 years have elapsed since the timer started.
sourcepub fn default_timeout(&self) -> Duration
pub fn default_timeout(&self) -> Duration
The default timeout. Timeout will be reset to this value upon a successful operation.
sourcepub fn set_default_timeout(&self, default_timeout: Duration)
pub fn set_default_timeout(&self, default_timeout: Duration)
Change the default timeout.
Warning: if this timeout is shorter than previous one, it will only update after the previous timeout has expired!
Panics
Panics if default_timeout
is longer than ~584 years