pub struct Deadline { /* private fields */ }Expand description
A monotonic-time deadline for cooperative task cancellation.
Build one with Deadline::after (relative) or Deadline::at
(absolute), then call Deadline::check_or_err between agent steps
and Deadline::remaining when handing the timeout to a per-call API.
Deadlines are immutable. To tighten a deadline for a nested operation
without touching the original, use Deadline::intersect.
Implementations§
Source§impl Deadline
impl Deadline
Sourcepub fn after(duration: Duration) -> Self
pub fn after(duration: Duration) -> Self
Build a deadline that fires duration from now.
A Duration::ZERO produces an already-expired deadline, which is
occasionally useful in tests.
Sourcepub fn at(instant: Instant) -> Self
pub fn at(instant: Instant) -> Self
Build a deadline that fires at an exact monotonic instant.
Use this when you already have an Instant from another source
(for example a parent task’s deadline expressed as an Instant).
Sourcepub fn never() -> Self
pub fn never() -> Self
A deadline that never fires.
Useful as a default for callers that may or may not want a cap.
Self::remaining returns Duration::MAX, Self::expired is
always false, and Self::check_or_err is a no-op.
Sourcepub fn is_never(&self) -> bool
pub fn is_never(&self) -> bool
true if this deadline was built with Deadline::never.
Sourcepub fn instant(&self) -> Option<Instant>
pub fn instant(&self) -> Option<Instant>
The absolute instant at which this deadline fires, if any.
Returns None for Deadline::never.
Sourcepub fn expired(&self) -> bool
pub fn expired(&self) -> bool
true once the current monotonic time is at or past the deadline.
Always false for Deadline::never.
Sourcepub fn remaining(&self) -> Duration
pub fn remaining(&self) -> Duration
Duration left until the deadline. Saturates at zero. Never negative.
Returns Duration::MAX for Deadline::never. Hand this to any
timeout-taking API.
Sourcepub fn remaining_seconds(&self) -> f64
pub fn remaining_seconds(&self) -> f64
Convenience for callers that want the remaining time as f64 seconds.
Returns f64::INFINITY for Deadline::never.
Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Seconds since this deadline was constructed.
Survives an Self::intersect: the elapsed counter still measures
against the original task start.
Sourcepub fn check_or_err(&self) -> Result<(), DeadlineExceeded>
pub fn check_or_err(&self) -> Result<(), DeadlineExceeded>
Cooperative check: return Err(DeadlineExceeded) if the deadline
has passed.
No-op for Deadline::never. Call between agent steps.
Sourcepub fn intersect(&self, other: &Deadline) -> Deadline
pub fn intersect(&self, other: &Deadline) -> Deadline
Return a new deadline that fires at the earlier of self and other.
created_at is preserved from self so Self::elapsed keeps
measuring against the original task start, not the intersection point.
Sourcepub fn intersect_after(&self, duration: Duration) -> Deadline
pub fn intersect_after(&self, duration: Duration) -> Deadline
Convenience: intersect with a deadline duration from now.
Trait Implementations§
impl Copy for Deadline
Source§impl Default for Deadline
impl Default for Deadline
Source§fn default() -> Self
fn default() -> Self
Default deadline is Deadline::never. Useful in struct fields
where “no cap” is the sane default.