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 Timeout<Runtime>
impl Timeout<Runtime>
Sourcepub fn new_tokio(default_timeout: Duration) -> Self
pub fn new_tokio(default_timeout: Duration) -> Self
Create a new timeout that expires after default_timeout
, creating a runtime with runtime::Tokio::new
§Panics
Panics if default_timeout
is longer than ~584 years
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!
Additionally, this won’t automatically reset the timeout - it will only affect the next reset.
§Panics
Panics if default_timeout
is longer than ~584 years