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.
§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§fn clone(&self) -> WithTimeout<T>
fn clone(&self) -> WithTimeout<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug> Debug for WithTimeout<T>
impl<T: Debug> Debug 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> 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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> StopExt for Twhere
T: Stop,
impl<T> StopExt for Twhere
T: Stop,
Source§impl<T> TimeoutExt for Twhere
T: Stop,
impl<T> TimeoutExt for Twhere
T: Stop,
Source§fn with_timeout(self, duration: Duration) -> WithTimeout<Self>
fn with_timeout(self, duration: Duration) -> WithTimeout<Self>
Add a timeout to this stop. Read more
Source§fn with_deadline(self, deadline: Instant) -> WithTimeout<Self>
fn with_deadline(self, deadline: Instant) -> WithTimeout<Self>
Add an absolute deadline to this stop. Read more