SuspendUnawareInstant

Struct SuspendUnawareInstant 

Source
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

  1. 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.
  2. If an instant in the future is subtracted from an instant in the past, we return a Duration of 0.
  3. If a duration is subtracted that would cause an instant to be negative, we return an instant set at 0.
  4. 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:

PlatformSystem call
UNIXclock_gettime (CLOCK_UPTIME_RAW)
Darwinclock_gettime (CLOCK_UPTIME_RAW)
VXWorksclock_gettime (CLOCK_UPTIME_RAW)
WindowsQueryUnbiasedInterruptTimePrecise

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

Source

pub fn now() -> SuspendUnawareInstant

Returns an instant corresponding to “now”.

§Examples
use suspend_time::SuspendUnawareInstant;

let now = SuspendUnawareInstant::now();
Source

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

Source§

type Output = SuspendUnawareInstant

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Duration) -> SuspendUnawareInstant

Performs the + operation. Read more
Source§

impl Clone for SuspendUnawareInstant

Source§

fn clone(&self) -> SuspendUnawareInstant

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SuspendUnawareInstant

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for SuspendUnawareInstant

Source§

fn cmp(&self, other: &SuspendUnawareInstant) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SuspendUnawareInstant

Source§

fn eq(&self, other: &SuspendUnawareInstant) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for SuspendUnawareInstant

Source§

fn partial_cmp(&self, other: &SuspendUnawareInstant) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub<Duration> for SuspendUnawareInstant

Source§

type Output = SuspendUnawareInstant

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Duration) -> SuspendUnawareInstant

Performs the - operation. Read more
Source§

impl Sub for SuspendUnawareInstant

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: SuspendUnawareInstant) -> Duration

Performs the - operation. Read more
Source§

impl Copy for SuspendUnawareInstant

Source§

impl Eq for SuspendUnawareInstant

Source§

impl StructuralPartialEq for SuspendUnawareInstant

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.