Skip to main content

Instant

Trait Instant 

Source
pub trait Instant: Copy + Clone {
    // Required methods
    fn duration_since(&self, earlier: Self) -> Duration;
    fn add_duration(&self, duration: Duration) -> Self;
}
Expand description

An instant in time, used for measuring elapsed durations.

This trait abstracts over the time source so that users can provide their own clock implementations (e.g., for simulated time in tests, or for integration with async runtimes that provide their own Instant type).

The crate never calls “now” internally — callers always pass a timestamp into every method. This trait only needs to support computing the elapsed time between two instants and advancing an instant by a duration.

§Built-in implementation

std::time::Instant implements this trait, so the crate works out of the box with the standard library clock. A type alias LatencyTracker defaults the clock parameter to Instant for ergonomic use.

§Example: custom clock

use std::time::Duration;
use adaptive_timeout::Instant;

#[derive(Clone, Copy)]
struct FakeInstant(u64); // nanoseconds

impl Instant for FakeInstant {
    fn duration_since(&self, earlier: Self) -> Duration {
        Duration::from_nanos(self.0.saturating_sub(earlier.0))
    }
    fn add_duration(&self, duration: Duration) -> Self {
        FakeInstant(self.0 + duration.as_nanos() as u64)
    }
}

Required Methods§

Source

fn duration_since(&self, earlier: Self) -> Duration

Returns the duration elapsed from earlier to self.

Equivalent to self - earlier for std::time::Instant. If self is before earlier (clock skew), the implementation should return Duration::ZERO rather than panicking.

Source

fn add_duration(&self, duration: Duration) -> Self

Returns a new instant that is duration later than self.

Equivalent to self + duration for std::time::Instant.

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.

Implementations on Foreign Types§

Source§

impl Instant for Instant

Source§

fn duration_since(&self, earlier: Self) -> Duration

Source§

fn add_duration(&self, duration: Duration) -> Self

Implementors§