pub struct WithTimeout<T> { /* private fields */ }Expand description
A Stop wrapper that adds a deadline.
The wrapped stop will return StopReason::TimedOut if the deadline
passes, or propagate the inner stop’s reason if it stops first.
§Cost
Every check reads the clock (Instant::now()). Measured with perf on a
loop that checks after every 1 KiB of PNG defiltering (record), that is
about 112 instructions per check, and the loop took 22% more cycles on
x86-64 (12% on a Neoverse-N1); a plain Stopper measured within noise. In
return it stops no later than one check after the deadline.
If a library checks that often and the deadline doesn’t need that
precision, DebouncedTimeout reads the clock only every N checks, at
most 64, with N timed from two consecutive checks so that reads come about
once per 100 µs. After checks slow down it can stop up to 64 of the slower
checks late, unless the code holding it calls
clear_calibration where the pace
changes.
§Example
use almost_enough::{StopSource, Stop};
use almost_enough::time::WithTimeout;
use std::time::Duration;
let source = StopSource::new();
let timeout = WithTimeout::new(source.as_ref(), Duration::from_millis(100));
assert!(!timeout.should_stop());
std::thread::sleep(Duration::from_millis(150));
assert!(timeout.should_stop());Implementations§
Source§impl<T: Stop> WithTimeout<T>
impl<T: Stop> WithTimeout<T>
Sourcepub fn new(inner: T, duration: Duration) -> Self
pub fn new(inner: T, duration: Duration) -> Self
Create a new timeout wrapper.
The deadline is calculated as Instant::now() + duration.
Sourcepub fn with_deadline(inner: T, deadline: Instant) -> Self
pub fn with_deadline(inner: T, deadline: Instant) -> Self
Create a timeout wrapper with an absolute deadline.
Sourcepub fn remaining(&self) -> Duration
pub fn remaining(&self) -> Duration
Get the remaining time until deadline.
Returns Duration::ZERO if the deadline has passed.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap and return the inner stop.
Source§impl<T: Stop> WithTimeout<T>
impl<T: Stop> WithTimeout<T>
Sourcepub fn tighten(self, duration: Duration) -> Self
pub fn tighten(self, duration: Duration) -> Self
Add another timeout, taking the tighter of the two deadlines.
This prevents timeout nesting by updating the deadline in place.
Sourcepub fn tighten_deadline(self, deadline: Instant) -> Self
pub fn tighten_deadline(self, deadline: Instant) -> Self
Add another deadline, taking the earlier of the two.
This prevents timeout nesting by updating the deadline in place.
Trait Implementations§
Source§impl<T: Clone> Clone for WithTimeout<T>
impl<T: Clone> Clone for WithTimeout<T>
Source§impl<T: Debug> Debug for WithTimeout<T>
impl<T: Debug> Debug for WithTimeout<T>
Source§impl<T: Stop> Stop for WithTimeout<T>
impl<T: Stop> Stop for WithTimeout<T>
Auto Trait Implementations§
impl<T> Freeze for WithTimeout<T>where
T: Freeze,
impl<T> RefUnwindSafe for WithTimeout<T>where
T: RefUnwindSafe,
impl<T> Send for WithTimeout<T>where
T: Send,
impl<T> Sync for WithTimeout<T>where
T: Sync,
impl<T> Unpin for WithTimeout<T>where
T: Unpin,
impl<T> UnsafeUnpin for WithTimeout<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for WithTimeout<T>where
T: UnwindSafe,
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
impl<T> CloneStop for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DebouncedTimeoutExt for Twhere
T: Stop,
impl<T> DebouncedTimeoutExt for Twhere
T: Stop,
Source§fn with_debounced_timeout(self, duration: Duration) -> DebouncedTimeout<Self>
fn with_debounced_timeout(self, duration: Duration) -> DebouncedTimeout<Self>
Source§fn with_debounced_deadline(self, deadline: Instant) -> DebouncedTimeout<Self>
fn with_debounced_deadline(self, deadline: Instant) -> DebouncedTimeout<Self>
Source§impl<T> StopExt for Twhere
T: Stop,
impl<T> StopExt for Twhere
T: Stop,
Source§fn or<S: Stop>(self, other: S) -> OrStop<Self, S>
fn or<S: Stop>(self, other: S) -> OrStop<Self, S>
Source§fn into_boxed(self) -> BoxedStopwhere
Self: 'static,
fn into_boxed(self) -> BoxedStopwhere
Self: 'static,
use into_token(), whose StopToken checks the same way and is Clone
BoxedStop. Deprecated: use
into_token(), whose StopToken checks the
same way and is Clone.