pub struct SuspendUnawareInstant { /* private fields */ }Expand description
Similar to the standard library’s implementation of
Instant,
except it is consistently unaware of system suspends across all platforms
supported by this library.
Historically, this has been inconsistent in the standard library, with windows allowing time to pass when the system is suspended/hibernating, however unix systems do not “pass time” during system suspension. In this library, time never passes when the system is suspended on any platform.
This instant implementation is:
- Opaque (you cannot manually create an Instant. You must call ::now())
- Cross platform (windows, macOS)
- Monotonic (time never goes backwards)
- Suspend-unaware (when you put your computer to sleep, “time” does not pass.)
§Undefined behavior / Invariants
- When polling the system clock, nanoseconds should never exceed 10^9 (the number of nanoseconds in 1 second). If this happens, we simply return zero. The standard library has a similar invariant (0 <= nanos <= 10^9), but handles it differently.
- If an instant in the future is subtracted from an instant in the past, we return a Duration of 0.
- If a duration is subtracted that would cause an instant to be negative, we return an instant set at 0.
- If a duration is added to an instant that would cause the instant to exceed 2^64 seconds, we return an instant set to 0.
§Underlying System calls
The following system calls are currently being used by now() to find out
the current time:
| Platform | System call |
|---|---|
| UNIX | clock_gettime (CLOCK_UPTIME_RAW) |
| Darwin | clock_gettime (CLOCK_UPTIME_RAW) |
| VXWorks | clock_gettime (CLOCK_UPTIME_RAW) |
| Windows | QueryUnbiasedInterruptTimePrecise |
Certain overflows are dependent on how the standard library implements Duration. For example, right now it is implemented as a u64 counting seconds. As such, to prevent overflow we must check if the number of seconds in two Durations exceeds the bounds of a u64. To avoid being dependent on the standard library for cases like this, we choose our own representation of time which matches the “apple” libc platform implementation.
Implementations§
Source§impl SuspendUnawareInstant
impl SuspendUnawareInstant
Sourcepub fn now() -> SuspendUnawareInstant
pub fn now() -> SuspendUnawareInstant
Returns an instant corresponding to “now”.
§Examples
use suspend_time::SuspendUnawareInstant;
let now = SuspendUnawareInstant::now();Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the amount of system unsuspended time elapsed since this suspend unaware instant was created, or zero duration if that this instant is in the future.
§Examples
use std::{thread, time};
use suspend_time::{SuspendUnawareInstant};
let instant = SuspendUnawareInstant::now();
let one_sec = time::Duration::from_secs(1);
thread::sleep(one_sec);
assert!(instant.elapsed() >= one_sec);Trait Implementations§
Source§impl Add<Duration> for SuspendUnawareInstant
impl Add<Duration> for SuspendUnawareInstant
Source§type Output = SuspendUnawareInstant
type Output = SuspendUnawareInstant
+ operator.Source§impl Clone for SuspendUnawareInstant
impl Clone for SuspendUnawareInstant
Source§fn clone(&self) -> SuspendUnawareInstant
fn clone(&self) -> SuspendUnawareInstant
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SuspendUnawareInstant
impl Debug for SuspendUnawareInstant
Source§impl Ord for SuspendUnawareInstant
impl Ord for SuspendUnawareInstant
Source§fn cmp(&self, other: &SuspendUnawareInstant) -> Ordering
fn cmp(&self, other: &SuspendUnawareInstant) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for SuspendUnawareInstant
impl PartialEq for SuspendUnawareInstant
Source§impl PartialOrd for SuspendUnawareInstant
impl PartialOrd for SuspendUnawareInstant
Source§impl Sub<Duration> for SuspendUnawareInstant
impl Sub<Duration> for SuspendUnawareInstant
Source§type Output = SuspendUnawareInstant
type Output = SuspendUnawareInstant
- operator.