pub struct Instant(/* private fields */);
Expand description
A measurement of a monotonically nondecreasing clock. Similar to
std::time::Instant
but is faster and more
accurate if TSC is available.
Implementations§
source§impl Instant
impl Instant
sourcepub fn now() -> Instant
pub fn now() -> Instant
Returns an instant corresponding to “now”.
§Examples
use minstant::Instant;
let now = Instant::now();
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.
§Panics
Previously we panicked if earlier
was later than self
. Currently this method saturates
to follow the behavior of the standard library. Future versions may reintroduce the panic
in some circumstances.
§Examples
use std::time::Duration;
use std::thread::sleep;
use minstant::Instant;
let now = Instant::now();
sleep(Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.duration_since(now));
println!("{:?}", now.duration_since(new_now)); // 0ns
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.
§Examples
use std::time::Duration;
use std::thread::sleep;
use minstant::Instant;
let now = Instant::now();
sleep(Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.checked_duration_since(now));
println!("{:?}", now.checked_duration_since(new_now)); // None
sourcepub fn saturating_duration_since(&self, earlier: Instant) -> Duration
pub fn saturating_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.
§Examples
use std::time::Duration;
use std::thread::sleep;
use minstant::Instant;
let now = Instant::now();
sleep(Duration::new(1, 0));
let new_now = Instant::now();
println!("{:?}", new_now.saturating_duration_since(now));
println!("{:?}", now.saturating_duration_since(new_now)); // 0ns
sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the amount of time elapsed since this instant was created.
§Panics
This function may panic if the current time is earlier than this
instant, which is something that can happen if an Instant
is
produced synthetically.
§Examples
use std::time::Duration;
use std::thread::sleep;
use minstant::Instant;
let instant = Instant::now();
let three_secs = Duration::from_secs(3);
sleep(three_secs);
assert!(instant.elapsed() >= three_secs);
sourcepub fn checked_add(&self, duration: Duration) -> Option<Instant>
pub fn checked_add(&self, duration: Duration) -> Option<Instant>
Returns Some(t)
where t
is the time self + duration
if t
can be represented as
Instant
(which means it’s inside the bounds of the underlying data structure), None
otherwise.
sourcepub fn checked_sub(&self, duration: Duration) -> Option<Instant>
pub fn checked_sub(&self, duration: Duration) -> Option<Instant>
Returns Some(t)
where t
is the time self - duration
if t
can be represented as
Instant
(which means it’s inside the bounds of the underlying data structure), None
otherwise.
sourcepub fn as_unix_nanos(&self, anchor: &Anchor) -> u64
pub fn as_unix_nanos(&self, anchor: &Anchor) -> u64
Convert interal clocking counter into a UNIX timestamp represented as the nanoseconds elapsed from UNIX_EPOCH.
Anchor
contains the necessary calibration data for conversion.
Typically, initializing an Anchor
takes about 50 nano seconds, so
try to reuse it for a batch of Instant
.
§Examples
use std::time::UNIX_EPOCH;
use minstant::{Instant, Anchor};
let anchor = Anchor::new();
let instant = Instant::now();
let expected = UNIX_EPOCH.elapsed().unwrap().as_nanos();
assert!((instant.as_unix_nanos(&anchor) as i64 - expected as i64).abs() < 1_000_000);
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 Sub for Instant
impl Sub for Instant
source§fn sub(self, other: Instant) -> Duration
fn sub(self, other: 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.
§Panics
Previously we panicked if other
was later than self
. Currently this method saturates
to follow the behavior of the standard library. Future versions may reintroduce the panic
in some circumstances.
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 moreimpl Copy for Instant
impl Eq for Instant
impl StructuralPartialEq for Instant
Auto Trait Implementations§
impl Freeze for Instant
impl RefUnwindSafe for Instant
impl Send for Instant
impl Sync for Instant
impl Unpin for Instant
impl UnwindSafe for Instant
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)