ClockHandle

Struct ClockHandle 

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

A handle to a clock for getting time and performing time-based operations.

This is the main interface for time operations. It’s cheap to clone and can be shared across tasks and threads. All clones share the same underlying clock, so they see consistent time.

§Creating a Clock

use es_entity::clock::{ClockHandle, ArtificialClockConfig};

// Real-time clock for production
let clock = ClockHandle::realtime();

// Artificial clock for testing - returns (handle, controller)
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig::manual());

§Basic Operations

use es_entity::clock::ClockHandle;
use std::time::Duration;

let clock = ClockHandle::realtime();

// Get current time
let now = clock.now();

// Sleep for a duration
clock.sleep(Duration::from_secs(1)).await;

// Timeout a future
match clock.timeout(Duration::from_secs(5), some_async_operation()).await {
    Ok(result) => println!("Completed: {:?}", result),
    Err(_) => println!("Timed out"),
}

Implementations§

Source§

impl ClockHandle

Source

pub fn realtime() -> Self

Create a real-time clock that uses the system clock and tokio timers.

Source

pub fn artificial(config: ArtificialClockConfig) -> (Self, ClockController)

Create an artificial clock with the given configuration.

Returns a tuple of (ClockHandle, ClockController). The handle provides the common time interface, while the controller provides operations for advancing time.

§Example
use es_entity::clock::{ClockHandle, ArtificialClockConfig, ArtificialMode};
use chrono::Utc;

// Manual mode - time only advances via controller.advance()
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig::manual());

// Auto mode - time advances 1000x faster than real time
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig::auto(1000.0));

// Start at a specific time
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig {
    start_at: Utc::now() - chrono::Duration::days(30),
    mode: ArtificialMode::Manual,
});
Source

pub fn now(&self) -> DateTime<Utc>

Get the current time.

This is a fast, synchronous operation regardless of clock type.

For real-time clocks, this returns Utc::now(). For artificial clocks, this returns the current artificial time.

Source

pub fn sleep(&self, duration: Duration) -> ClockSleep

Sleep for the given duration.

For real-time clocks, this delegates to tokio::time::sleep. For artificial clocks in manual mode, this waits until time is advanced. For artificial clocks in auto mode, this sleeps for a scaled real duration.

Source

pub fn timeout<F>(&self, duration: Duration, future: F) -> ClockTimeout<F>
where F: Future,

Apply a timeout to a future.

Returns Ok(output) if the future completes before the timeout, or Err(Elapsed) if the timeout expires first.

Source

pub fn is_artificial(&self) -> bool

Check if this clock is artificial (as opposed to realtime).

Source

pub fn artificial_now(&self) -> Option<DateTime<Utc>>

Get the current artificial time, if this is an artificial clock that hasn’t transitioned to realtime.

Returns:

  • None for realtime clocks
  • None for artificial clocks that have transitioned to realtime
  • Some(time) for artificial clocks (manual or auto) that are still artificial

This is useful for code that needs to cache time when running under artificial clocks but use fresh time for realtime clocks.

Trait Implementations§

Source§

impl Clone for ClockHandle

Source§

fn clone(&self) -> ClockHandle

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 ClockHandle

Source§

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

Formats the value using the given formatter. Read more

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more