pub struct Instant(/* private fields */);
Expand description
A measurement of a monotonically nondecreasing clock.
Opaque and useful only with Duration
.
Instants 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 may 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.
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 was created.
§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 PartialEq<Instant> for Instant
impl PartialEq<Instant> for Instant
source§impl PartialEq<Instant> for Instant
impl PartialEq<Instant> for Instant
source§impl PartialEq for Instant
impl PartialEq for Instant
source§impl PartialOrd<Instant> for Instant
impl PartialOrd<Instant> 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 PartialOrd<Instant> for Instant
impl PartialOrd<Instant> 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 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 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