pub struct Instant(/* private fields */);std only.Expand description
A measurement of a monotonically nondecreasing clock.
Opaque and useful only with Duration.
Instants are always guaranteed, barring platform bugs, to be no less than any previously measured instant when created, and are often useful for tasks such as measuring benchmarks or timing how long an operation takes.
Note, however, that instants are not guaranteed to be steady. In other words, each tick of the underlying clock might not be the same length (e.g. some seconds may be longer than others). An instant may jump forwards or experience time dilation (slow down or speed up), but it will never go backwards. As part of this non-guarantee it is also not specified whether system suspends count as elapsed time or not. The behavior varies across platforms and Rust versions.
Instants are opaque types that can only be compared to one another. There is no method to get “the number of seconds” from an instant. Instead, it only allows measuring the duration between two instants (or comparing two instants).
The size of an Instant struct may vary depending on the target operating
system.
§OS-specific behaviors
An Instant is a wrapper around system-specific types and it may behave
differently depending on the underlying operating system.
See the standard library documentation for more.
§Underlying System calls
See the standard library documentation
for the system calls used to get the current time using now().
Implementations§
Source§impl Instant
impl Instant
Sourcepub fn now() -> Self
pub fn now() -> Self
Returns an instant corresponding to “now”.
§Examples
use easytime::Instant;
let now = Instant::now();Sourcepub fn duration_since(&self, earlier: Self) -> Duration
pub fn duration_since(&self, earlier: Self) -> Duration
Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
§Examples
use std::{thread::sleep, time};
use easytime::Instant;
let now = Instant::now();
sleep(time::Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.duration_since(now));
println!("{:?}", now.duration_since(new_now)); // Some(0ns)Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the amount of time elapsed since this instant.
§Examples
use std::{thread::sleep, time};
use easytime::Instant;
let instant = Instant::now();
let three_secs = time::Duration::from_secs(3);
sleep(three_secs);
assert!(instant.elapsed() >= three_secs);Sourcepub const fn is_some(&self) -> bool
pub const fn is_some(&self) -> bool
Returns true if into_inner returns Some.
Sourcepub const fn is_none(&self) -> bool
pub const fn is_none(&self) -> bool
Returns true if into_inner returns None.
Sourcepub const fn into_inner(self) -> Option<Instant>
pub const fn into_inner(self) -> Option<Instant>
Returns the contained std::time::Instant or None.
Sourcepub const fn unwrap_or(self, default: Instant) -> Instant
pub const fn unwrap_or(self, default: Instant) -> Instant
Returns the contained std::time::Instant or a default.
instant.unwrap_or(default) is equivalent to instant.into_inner().unwrap_or(default).
Sourcepub fn unwrap_or_else<F>(self, default: F) -> Instant
pub fn unwrap_or_else<F>(self, default: F) -> Instant
Returns the contained std::time::Instant or computes it from a closure.
instant.unwrap_or_else(default) is equivalent to instant.into_inner().unwrap_or_else(default).
Trait Implementations§
Source§impl AddAssign<Duration> for Instant
impl AddAssign<Duration> for Instant
Source§fn add_assign(&mut self, other: Duration)
fn add_assign(&mut self, other: Duration)
+= operation. Read moreSource§impl AddAssign<Duration> for Instant
impl AddAssign<Duration> for Instant
Source§fn add_assign(&mut self, other: Duration)
fn add_assign(&mut self, other: Duration)
+= operation. Read moreSource§impl Ord for Instant
impl Ord for Instant
Source§impl PartialOrd<Instant> for Instant
impl PartialOrd<Instant> for Instant
Source§impl PartialOrd<Instant> for Instant
impl PartialOrd<Instant> for Instant
Source§impl PartialOrd for Instant
impl PartialOrd for Instant
Source§impl SubAssign<Duration> for Instant
impl SubAssign<Duration> for Instant
Source§fn sub_assign(&mut self, other: Duration)
fn sub_assign(&mut self, other: Duration)
-= operation. Read moreSource§impl SubAssign<Duration> for Instant
impl SubAssign<Duration> for Instant
Source§fn sub_assign(&mut self, other: Duration)
fn sub_assign(&mut self, other: Duration)
-= operation. Read more