Instant

Struct Instant 

Source
pub struct Instant { /* private fields */ }
Expand description

An instant returned by the fast-time clock.

Represents a specific point in time captured from a Clock. Use this to measure elapsed time or to compare with other instants from the same clock.

You may convert the instant into an std::time::Instant for arithmetic operations and interoperability with standard library code.

§Examples

Basic elapsed time measurement:

use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let start = clock.now();

// Simulate some work
std::thread::sleep(Duration::from_millis(10));

let elapsed = start.elapsed(&mut clock);
// Note: fast_time prioritizes efficiency over precision
assert!(elapsed <= Duration::from_secs(1)); // Generous upper bound

Comparing instants:

use fast_time::Clock;

let mut clock = Clock::new();
let instant1 = clock.now();
let instant2 = clock.now();

// instant2 should be later than instant1
assert!(instant2 >= instant1);

let duration = instant2.saturating_duration_since(instant1);
println!("Time between captures: {:?}", duration);

Converting to standard library types:

use std::time::Instant as StdInstant;

use fast_time::Clock;

let mut clock = Clock::new();
let fast_instant = clock.now();

// Convert to std::time::Instant
let std_instant: StdInstant = fast_instant.into();

// Convert back
let converted_back: fast_time::Instant = std_instant.into();

Implementations§

Source§

impl Instant

Source

pub fn elapsed(&self, clock: &mut Clock) -> Duration

Calculates the elapsed time since this instant using the provided clock.

This method captures a new timestamp from the clock and calculates the duration that has passed since this instant was created.

§Examples
use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let start = clock.now();

// Simulate some work
std::thread::sleep(Duration::from_millis(5));

let elapsed = start.elapsed(&mut clock);
// Note: fast_time prioritizes efficiency over precision
assert!(elapsed <= Duration::from_secs(1)); // Generous upper bound
Source

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

Calculates the duration since an earlier instant.

Returns the amount of time that passed between the earlier instant and this instant. If the earlier instant is actually later than this instant, returns a duration of zero (saturating behavior).

§Examples
use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let start = clock.now();

// Simulate some work
std::thread::sleep(Duration::from_millis(8));

let end = clock.now();
let duration = end.saturating_duration_since(start);
// Note: fast_time prioritizes efficiency over precision
assert!(duration <= Duration::from_secs(1)); // Generous upper bound

Saturating behavior with reversed order:

use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let instant1 = clock.now();
let instant2 = clock.now();

// instant1 is earlier, so this returns zero
let duration = instant1.saturating_duration_since(instant2);
assert_eq!(duration, Duration::ZERO);
Source

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

Calculates the duration since an earlier instant.

This is an alias for saturating_duration_since to maintain compatibility with std::time::Instant.

§Examples
use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let start = clock.now();
let end = clock.now();

let duration = end.duration_since(start);
Source

pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration>

Calculates the duration since an earlier instant, returning None if the earlier instant is actually later than this instant.

Unlike saturating_duration_since, this method returns None instead of zero when the earlier instant is later.

§Examples
use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let start = clock.now();

// Simulate some work to ensure time passes
std::thread::sleep(Duration::from_millis(10));

let end = clock.now();

// Normal case: end should be later than start
match end.checked_duration_since(start) {
    Some(duration) => println!("Elapsed: {:?}", duration),
    None => println!("Time went backwards"),
}

// Test with the same instant - should return Some(Duration::ZERO)
let same_instant = clock.now();
assert_eq!(
    same_instant.checked_duration_since(same_instant),
    Some(Duration::ZERO)
);
Source

pub fn checked_add(&self, duration: Duration) -> Option<Self>

Adds a duration to this instant, returning None if overflow occurred.

§Examples
use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let instant = clock.now();

let later = instant.checked_add(Duration::from_secs(5)).unwrap();
assert!(later > instant);

// This would overflow
assert!(instant.checked_add(Duration::MAX).is_none());
Source

pub fn checked_sub(&self, duration: Duration) -> Option<Self>

Subtracts a duration from this instant, returning None if underflow occurred.

§Examples
use std::time::Duration;

use fast_time::Clock;

let mut clock = Clock::new();
let instant = clock.now();

let earlier = instant.checked_sub(Duration::from_secs(5)).unwrap();
assert!(earlier < instant);

// This would underflow
assert!(instant.checked_sub(Duration::MAX).is_none());

Trait Implementations§

Source§

impl Clone for Instant

Source§

fn clone(&self) -> Instant

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Instant

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Instant> for Instant

Source§

fn from(inner: Instant) -> Self

Converts to this type from the input type.
Source§

impl From<Instant> for Instant

Source§

fn from(instant: Instant) -> Self

Converts to this type from the input type.
Source§

impl Hash for Instant

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Instant

Source§

fn cmp(&self, other: &Instant) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Instant

Source§

fn eq(&self, other: &Instant) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Instant

Source§

fn partial_cmp(&self, other: &Instant) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Instant

Source§

impl Eq for Instant

Source§

impl StructuralPartialEq for Instant

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.