Skip to main content

Clock

Struct Clock 

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

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, an advance to the deadline sends it, and the channel stays connected while the clock lives. 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 to the deadline sends it, even one that overshoots, and the channel stays connected while the clock lives.

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