pub enum TimeService {
RealTimeService(RealTimeService),
}Expand description
TimeService abstracts all time-related operations in one place that can be
easily mocked-out and controlled in tests or delegated to the actual
underlying runtime (usually tokio). It’s provided as an enum so we don’t
have to infect everything with a generic tag.
TimeService is async-focused: the sleep, interval, and timeout
methods all return Futures and Streams. That said, TimeService
supports non-async clients as well; simply use the sleep_blocking method
instead of sleep. Note that the blocking call will actually block the
current thread until the sleep time has elapsed.
TimeService tries to mirror the API provided by tokio::time to an
extent. The primary difference is that all time is expressed in relative
Durations. In other words, “sleep for 5s” vs “sleep until unix time
1607734460”. Absolute time is provided by TimeService::now which returns
the current unix time.
Note: you must also include the TimeServiceTrait to use the actual
time-related functionality.
Note: we have to provide our own Timeout and Interval types that
use the Sleep future, since tokio’s implementations are coupled to its
internal Sleep future.
Note: TimeService’s should be free (or very cheap) to clone and send
around between threads. In production (without test features), this enum is
a zero-sized type.
Variants§
RealTimeService(RealTimeService)
Implementations§
Source§impl TimeService
impl TimeService
Sourcepub fn real() -> Self
pub fn real() -> Self
Create a new real, production time service that actually uses the systemtime.
See RealTimeService.
Trait Implementations§
Source§impl Clone for TimeService
impl Clone for TimeService
Source§fn clone(&self) -> TimeService
fn clone(&self) -> TimeService
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TimeService
impl Debug for TimeService
Source§impl Default for TimeService
impl Default for TimeService
Source§impl From<RealTimeService> for TimeService
impl From<RealTimeService> for TimeService
Source§fn from(v: RealTimeService) -> TimeService
fn from(v: RealTimeService) -> TimeService
Source§impl TimeServiceTrait for TimeService
impl TimeServiceTrait for TimeService
Source§fn now(&self) -> Instant
fn now(&self) -> Instant
Query a monotonically nondecreasing clock. Returns an opaque type that
can only be compared to other Instants, i.e., this is a monotonic
relative time whereas now_unix_time is a
non-monotonic absolute time.
On Linux, this is equivalent to
clock_gettime(CLOCK_MONOTONIC, _)
See Instant for more details.
Source§fn now_unix_time(&self) -> Duration
fn now_unix_time(&self) -> Duration
Query the current unix timestamp as a Duration.
When used on a TimeService::real(), this is equivalent to
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).
Note: the Duration returned from this function is NOT guaranteed
to be monotonic. Use now if you need monotonicity.
From the SystemTime docs:
Distinct from the
Instanttype, this time measurement is not monotonic. This means that you can save a file to the file system, then save another file to the file system, and the second file has aSystemTimemeasurement earlier than the first. In other words, an operation that happens after another operation in real time may have an earlier SystemTime!
For example, the system administrator could clock_settime into the
past, breaking clock time monotonicity.
On Linux, this is equivalent to
clock_gettime(CLOCK_REALTIME, _).
Source§fn now_secs(&self) -> u64
fn now_secs(&self) -> u64
Query the current unix timestamp in seconds.
Equivalent to self.now_unix_time().as_secs().
See now_unix_time.
Source§fn sleep_blocking(&self, __enum_dispatch_arg_0: Duration)
fn sleep_blocking(&self, __enum_dispatch_arg_0: Duration)
Blocks the current thread until duration time has passed.