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 with stable TSC.
Implementations§
Source§impl Instant
impl Instant
Sourcepub fn raw(&self) -> u64
pub fn raw(&self) -> u64
Returns the raw value of the tsc counter.
§Examples
use rdtsc_time::Instant;
let ddl = Instant::now().raw().checked_add(rdtsc_time::cycles_per_sec()).unwrap();
while Instant::now().raw() <= ddl {}
println!("1s has passed");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)); // 0nsSourcepub 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)); // NoneSourcepub 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)); // 0nsSourcepub 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 PartialOrd for Instant
impl PartialOrd for Instant
Source§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 more