TimespecInstant

Struct TimespecInstant 

Source
pub struct TimespecInstant { /* private fields */ }
Expand description

An Instant type inspired by the timespec struct from the C standard library

This instant type is implemented in a similar way as the Duration type because they both use a struct of seconds and (subsecond) nanoseconds to represent time. This has the great benefit that additions and subtractions do not require divisions, see the documentation to Instant32 and Instant64.

The main drawback of this instant type might be that creating these instants is harder. It can be done in basically two ways:

  1. The underlying clock uses a simple integer tick counter. In these cases, creating each instant requires the division which is saved when working with the instants. Depending on the use case, this might be more or less efficient than using Instant32 or Instant64.
  2. The underlying clock works on a similar seconds+nanoseconds struct directly. In these cases, creating an instant is easy but incrementing the underlying value in the clock tick interrupt gets harder. It is harder because the clock tick interrupt handler needs to take care of the nanosecond overflow which happens once per second and which needs to be properly synchronized with the contexts which request instants. Note though, that this might be the same problem as implementing a clock for Instant32 or Instant64 on platforms that do not support the corresponding atomic types. Especially on widespread 32 bit platforms, the AtomicU64 might be missing.

Also, if you are especially concerned about data sizes, Instant32 might be more appropriate for you because this type uses a 64 bit representation, see below.

The seconds and nanoseconds are both stored as u32 values. For the nanoseconds, this should be obvious. For the seconds, using a 32 bit integer might remind of the year 2038 problem but this is only relevant when working with wall clock times. Since Instant is about working with monotonic clocks which can always be measured since program or system start, this would only be relevant if the program or system was designed to run for longer than about 136 years. So this should affect nearly no users at all. Therefore, saving additional bits seems reasonable.

Implementations§

Source§

impl TimespecInstant

Source

pub fn new(secs: u32, nanos: u32) -> Self

Create a new TimespecInstant

Trait Implementations§

Source§

impl Add<Duration> for TimespecInstant

Source§

fn add(self, other: Duration) -> TimespecInstant

§Panics

This function may panic if the resulting point in time cannot be represented by the underlying data structure. See Instant::checked_add for a version without panic.

Source§

type Output = TimespecInstant

The resulting type after applying the + operator.
Source§

impl AddAssign<Duration> for TimespecInstant

Source§

fn add_assign(&mut self, other: Duration)

Performs the += operation. Read more
Source§

impl Clone for TimespecInstant

Source§

fn clone(&self) -> TimespecInstant

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 TimespecInstant

Source§

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

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

impl Hash for TimespecInstant

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Instant for TimespecInstant

Source§

fn checked_duration_since(&self, earlier: Self) -> Option<Duration>

Returns the amount of time elapsed from another instant to this one, or None if that instant is later than this one. Read more
Source§

fn checked_add(&self, duration: Duration) -> Option<Self>

Returns Some(t) where t is the time self + duration if t can be represented as Self (which means it’s inside the bounds of the underlying data structure), None otherwise.
Source§

fn checked_sub(&self, duration: Duration) -> Option<Self>

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.
Source§

fn duration_since(&self, earlier: Self) -> Duration

Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one. Read more
Source§

fn saturating_duration_since(&self, earlier: Self) -> Duration

Returns the amount of time elapsed from another instant to this one, or zero duration if that instant is later than this one. Read more
Source§

impl Ord for TimespecInstant

Source§

fn cmp(&self, other: &TimespecInstant) -> 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 TimespecInstant

Source§

fn eq(&self, other: &TimespecInstant) -> 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 TimespecInstant

Source§

fn partial_cmp(&self, other: &TimespecInstant) -> 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 TimespecInstant

Source§

type Output = TimespecInstant

The resulting type after applying the - operator.
Source§

fn sub(self, other: Duration) -> TimespecInstant

Performs the - operation. Read more
Source§

impl Sub for TimespecInstant

Source§

fn sub(self, other: TimespecInstant) -> 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

As in the current version of the Rust stdlib, this method saturates instead of panicking.

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

impl SubAssign<Duration> for TimespecInstant

Source§

fn sub_assign(&mut self, other: Duration)

Performs the -= operation. Read more
Source§

impl Copy for TimespecInstant

Source§

impl Eq for TimespecInstant

Source§

impl StructuralPartialEq for TimespecInstant

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, 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.