pub struct Instant(/* private fields */);
Expand description
A measurement of a monotonically nondecreasing Clock
.
Opaque and useful only with Duration
.
Provided that the Clock
implementation is correct, Instant
s are always
guaranteed 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.
Implementations§
source§impl Instant
impl Instant
sourcepub fn now() -> Instant
pub fn now() -> Instant
Returns an instant corresponding to “now”.
This function uses the global default timer. See the module-level documentation for details on using the global default timer.
Panics
This function panics if the global default timer has not been set.
For a version of this function that returns a Result
rather than
panicking, use Instant::try_now
instead.
sourcepub fn try_now() -> Result<Self, TimerError>
pub fn try_now() -> Result<Self, TimerError>
Returns an instant corresponding to “now”, without panicking.
This function uses the global default timer. See the module-level documentation for details on using the global default timer.
Returns
Ok
(
Instant
)
if the [global default timer] is available.Err
(
TimerError
)
if no global default timer has been set.
sourcepub fn duration_since(&self, earlier: Instant) -> Duration
pub fn duration_since(&self, earlier: Instant) -> Duration
Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one.
sourcepub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration>
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration>
Returns the amount of time elapsed from another instant to this one,
or None
` if that instant is later than this one.
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 Ord for Instant
impl Ord for Instant
source§impl PartialEq for Instant
impl PartialEq for Instant
source§impl PartialOrd for Instant
impl PartialOrd for Instant
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. 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