Clock

Struct Clock 

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

A clock that can efficiently provide the current time.

This is suitable for querying rapidly with low overhead, in circumstances where absolute precision is not necessary. The timestamps produced by this clock are monotonically increasing but may lag behind wall-clock time by a small number of milliseconds and may not follow explicit wall clock adjustments applied by the operating system (e.g. as part of clock synchronization).

This typically makes these timestamps useful for rapid polling scenarios, such as metrics and logging, where efficiency matters greatly because you may be capturing 100 000 timestamps per second, whereas the precise microsecond or even millisecond is not important.

§Examples

Creating a clock and capturing timestamps:

use fast_time::Clock;

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

// Timestamps are monotonically increasing
assert!(instant2.saturating_duration_since(instant1).as_nanos() >= 0);

Measuring elapsed time:

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, so we use loose tolerance
assert!(elapsed <= Duration::from_millis(50)); // Very generous upper bound

High-frequency timestamp collection:

use fast_time::Clock;

let mut clock = Clock::new();
let mut durations = Vec::new();

let start = clock.now();
for _ in 0..1000 {
    let timestamp = clock.now();
    durations.push(timestamp.saturating_duration_since(start));
}

// All durations should be monotonically increasing
for window in durations.windows(2) {
    assert!(window[1] >= window[0]);
}

Implementations§

Source§

impl Clock

Source

pub fn new() -> Self

Creates a new clock instance.

The clock will use the platform’s most efficient time source for rapid timestamp capture.

§Examples
use fast_time::Clock;

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

pub fn now(&mut self) -> Instant

Returns the current timestamp.

This method is optimized for rapid, repeated calls. The returned Instant represents a point in time that can be used to measure elapsed duration.

§Examples

Basic timestamp capture:

use fast_time::Clock;

let mut clock = Clock::new();
let now = clock.now();
println!("Current time: {:?}", now);

Rapid timestamp collection:

use fast_time::Clock;

let mut clock = Clock::new();
let timestamps: Vec<_> = (0..100).map(|_| clock.now()).collect();

// All timestamps should be valid
assert_eq!(timestamps.len(), 100);

Trait Implementations§

Source§

impl Debug for Clock

Source§

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

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

impl Default for Clock

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Clock

§

impl RefUnwindSafe for Clock

§

impl Send for Clock

§

impl Sync for Clock

§

impl Unpin for Clock

§

impl UnwindSafe for Clock

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> 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, 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.