pub trait TimeoutExt: Stop + Sized {
// Provided methods
fn with_timeout(self, duration: Duration) -> WithTimeout<Self> { ... }
fn with_deadline(self, deadline: Instant) -> WithTimeout<Self> { ... }
}Expand description
Extension trait for adding timeouts to any Stop implementation.
This trait is automatically implemented for all Stop types.
§Example
use almost_enough::{StopSource, Stop, TimeoutExt};
use std::time::Duration;
let source = StopSource::new();
let stop = source.as_ref().with_timeout(Duration::from_secs(30));
assert!(!stop.should_stop());Provided Methods§
Sourcefn with_timeout(self, duration: Duration) -> WithTimeout<Self>
fn with_timeout(self, duration: Duration) -> WithTimeout<Self>
Add a timeout to this stop.
The resulting stop will return StopReason::TimedOut if the
duration elapses before the operation completes.
§Timeout Tightening
If called multiple times, the earliest deadline wins:
use almost_enough::{StopSource, TimeoutExt};
use std::time::Duration;
let source = StopSource::new();
let stop = source.as_ref()
.with_timeout(Duration::from_secs(60))
.with_timeout(Duration::from_secs(10));
// Effective timeout is ~10 seconds
assert!(stop.remaining() < Duration::from_secs(11));Sourcefn with_deadline(self, deadline: Instant) -> WithTimeout<Self>
fn with_deadline(self, deadline: Instant) -> WithTimeout<Self>
Add an absolute deadline to this stop.
If called multiple times, the earliest deadline wins.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.