Skip to main content

Clock

Struct Clock 

Source
#[non_exhaustive]
pub struct Clock { /* private fields */ }
Expand description

A clock that reads real time, or a test’s time that moves only on command.

Clones share one clock. Equality compares identity, so all real clocks are equal and a test clock equals only the handles of its own TestClock.

It cannot be built from a struct literal and it has a destructor in every build, so code that compiles without test-clock compiles with it too.

Implementations§

Source§

impl Clock

Source

pub fn after(&self, duration: Duration) -> Receiver<Instant>

Available on crate feature crossbeam only.

Returns a receiver that gets the instant duration from now once this clock reaches it.

It mirrors crossbeam_channel::after, which it is on the real clock. On a test clock, it is Self::at for the instant duration from now. A duration past the end of Instant’s range returns a receiver that never fires.

Wait for a job or a timeout, driven from a test through wait_timers, since wait_blocked cannot see a thread blocked in select!:

use darkbio_clock::TestClock;
use darkbio_clock::crossbeam_channel::{select, unbounded};
use std::thread;
use std::time::Duration;

let mut tester = TestClock::new();
let clock = tester.clock();
let (_jobs, queue) = unbounded::<u32>();

let worker = thread::spawn(move || {
    select! {
        recv(queue) -> job => job.ok(),
        recv(clock.after(Duration::from_secs(5))) -> _ => None,
    }
});
tester.wait_timers(1);
tester.advance(Duration::from_secs(5));
assert_eq!(worker.join().unwrap(), None);
Source

pub fn at(&self, deadline: Instant) -> Receiver<Instant>

Available on crate feature crossbeam only.

Returns a receiver that gets deadline once this clock reaches it, at once if it already has.

It mirrors crossbeam_channel::at, which it is on the real clock. On a test clock, an advance that reaches the deadline delivers it, even one that overshoots, before any thread can read the new time. The channel stays connected until the TestClock and every Clock handle are gone.

An advance sends its due timers one at a time, in deadline order and then in the order they were armed, and a select! already waiting takes the first one sent. So a waiting select_biased! over timers due at the same instant takes the one armed first, where the real clock’s takes the first of their arms.

Source

pub fn recv_timeout<T>( &self, receiver: &Receiver<T>, timeout: Duration, ) -> Result<T, RecvTimeoutError>

Available on crate feature crossbeam only.

Receives a message, waiting up to timeout on this clock.

It mirrors crossbeam’s Receiver::recv_timeout, which it is on the real clock. A buffered message or a disconnection wins over the timeout. On a test clock, it is Self::recv_deadline for the instant timeout from now. A timeout past the end of Instant’s range waits like Receiver::recv.

Source

pub fn recv_deadline<T>( &self, receiver: &Receiver<T>, deadline: Instant, ) -> Result<T, RecvTimeoutError>

Available on crate feature crossbeam only.

Receives a message, waiting until this clock reaches deadline.

It mirrors crossbeam’s Receiver::recv_deadline, which it is on the real clock. A buffered message or a disconnection wins over the deadline. On a test clock, a clock timer due by the deadline holds its message before the receive can time out, so it wins too. A waiting receive arms a timer, which wait_timers counts until it fires or the receive returns.

Source§

impl Clock

Source

pub const fn real() -> Self

Returns the real clock, which reads the system’s monotonic and wall times.

Source

pub fn now(&self) -> Instant

Returns the clock’s current monotonic time.

Source

pub fn elapsed(&self, since: Instant) -> Duration

Returns the time since since, or zero if it is later than now.

Source

pub fn system_time(&self) -> SystemTime

Returns the clock’s current wall time.

Source

pub fn sleep(&self, duration: Duration)

Blocks the thread until duration has passed on this clock.

On a test clock, only its advances end the sleep.

§Panics

Panics on a test clock if the deadline overflows Instant.

Source

pub fn sleep_until(&self, deadline: Instant)

Blocks the thread until this clock reaches deadline, returning at once if it already has.

On a test clock, only its advances end the sleep.

Trait Implementations§

Source§

impl Clone for Clock

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Clock

Source§

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

Shows whether the clock is paused, its advance and its wall time.

Source§

impl Drop for Clock

Source§

fn drop(&mut self)

Does nothing, but gives every build a destructor, so enabling test-clock does not change which const contexts accept a clock.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for Clock

Source§

impl PartialEq for Clock

Source§

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

Compares identity, with all real clocks equal to each other.

1.0.0 (const: unstable) · Source§

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

Inequality operator !=. 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 UnsafeUnpin 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.