pub struct Instant(/* private fields */);
Expand description
A point-in-time wall-clock measurement.
Mimics most of the functionality of std::time::Instant
but provides an additional method for
using the “recent time” feature of quanta
.
§Monotonicity
On all platforms, Instant
will try to use an OS API that guarantees monotonic behavior if
available, which is the case for all supported platforms. In practice such guarantees are –
under rare circumstances – broken by hardware, virtualization or operating system bugs. To work
around these bugs and platforms not offering monotonic clocks duration_since
, elapsed
and sub
saturate to zero. In older quanta
versions this lead to a panic instead.
checked_duration_since
can be used to detect and handle situations where monotonicity is
violated, or Instant
s are subtracted in the wrong order.
This workaround obscures programming errors where earlier and later instants are accidentally
swapped. For this reason future quanta
versions may reintroduce panics.
Implementations§
source§impl Instant
impl Instant
sourcepub fn now() -> Instant
pub fn now() -> Instant
Gets the current time, scaled to reference time.
This method depends on a lazily initialized global clock, which can take up to 200ms to initialize and calibrate itself.
This method is the spiritual equivalent of Instant::now
. It is guaranteed to
return a monotonically increasing value.
sourcepub fn recent() -> Instant
pub fn recent() -> Instant
Gets the most recent current time, scaled to reference time.
This method provides ultra-low-overhead access to a slightly-delayed version of the current time. Instead of querying the underlying source clock directly, a shared, global value is read directly without the need to scale to reference time.
The value is updated by running an “upkeep” thread or by calling set_recent
. An
upkeep thread can be configured and spawned via Upkeep
.
If the upkeep thread has not been started, or no value has been set manually, a lazily initialized global clock will be used to get the current time. This clock can take up to 200ms to initialize and calibrate itself.
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.
§Panics
Previous versions of this method panicked when earlier was later than self
. Currently,
this method saturates to zero. Future versions may reintroduce the panic in some
circumstances. See Monotonicity.
§Examples
use quanta::Clock;
use std::time::Duration;
use std::thread::sleep;
let mut clock = Clock::new();
let now = clock.now();
sleep(Duration::new(1, 0));
let new_now = clock.now();
println!("{:?}", new_now.duration_since(now));
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 earlier than this one.
Due to monotonicity bugs, even under correct logical ordering of the passed Instant
s,
this method can return None
.
§Examples
use quanta::Clock;
use std::time::Duration;
use std::thread::sleep;
let mut clock = Clock::new();
let now = clock.now();
sleep(Duration::new(1, 0));
let new_now = clock.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 earlier than this one.
Due to monotonicity bugs, even under correct logical ordering of the passed Instant
s,
this method can return None
.
§Examples
use quanta::Clock;
use std::time::Duration;
use std::thread::sleep;
let mut clock = Clock::new();
let now = clock.now();
sleep(Duration::new(1, 0));
let new_now = clock.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
Previous quanta
versions panicked when the current time was earlier than self. Currently
this method returns a Duration of zero in that case. Future versions may reintroduce the
panic. See Monotonicity.
§Examples
use quanta::Clock;
use std::time::Duration;
use std::thread::sleep;
let mut clock = Clock::new();
let now = clock.now();
sleep(Duration::new(1, 0));
let elapsed = now.elapsed();
println!("{:?}", elapsed); // ≥ 1s
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.
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
Previous quanta
versions panicked when other
was later than self
. Currently this
method saturates. Future versions may reintroduce the panic in some circumstances.
See Monotonicity.
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: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)