Instant32

Struct Instant32 

Source
pub struct Instant32<const TICKS_PER_SEC: u32> { /* private fields */ }
Expand description

An Instant type using a single unsigned integer tick counter

For this type, it might be easy to create these instants and to handle the underlying clock value, especially if the corresponding atomic type is available, see the documentation to TimespecInstant.

The main drawback might be that all additions and subtractions require divisions which might be inefficient on some platforms (when using an integer type larger than the word width of the processor):

  1. Subtracting two instants yields a Duration which is a struct of seconds and (subsecond) nanoseconds. So this requires division and modulo to convert the simple tick difference into a Duration.
  2. Adding/subtracting a Duration to/from an instant requires to convert the seconds and nanoseconds into the corresponding ticks. Converting the seconds only requires multiplication with TICKS_PER_SEC, but converting the nanoseconds requires division by nanos_per_sec (which is calculated internally). This division is only a 32 bit division though.

This instant type is generic on the clock frequency TICKS_PER_SEC. For example, in order to implement a millisecond clock, set TICKS_PER_SEC to 1000.

Implementations§

Source§

impl<const TICKS_PER_SEC: u32> Instant32<TICKS_PER_SEC>

Source

pub fn new(ticks: u32) -> Self

Create a new Instant from the given number of ticks

Trait Implementations§

Source§

impl<const TICKS_PER_SEC: u32> Add<Duration> for Instant32<TICKS_PER_SEC>

Source§

fn add(self, other: Duration) -> Instant32<TICKS_PER_SEC>

§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 = Instant32<TICKS_PER_SEC>

The resulting type after applying the + operator.
Source§

impl<const TICKS_PER_SEC: u32> AddAssign<Duration> for Instant32<TICKS_PER_SEC>

Source§

fn add_assign(&mut self, other: Duration)

Performs the += operation. Read more
Source§

impl<const TICKS_PER_SEC: u32> Clone for Instant32<TICKS_PER_SEC>

Source§

fn clone(&self) -> Instant32<TICKS_PER_SEC>

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<const TICKS_PER_SEC: u32> Debug for Instant32<TICKS_PER_SEC>

Source§

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

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

impl<const TICKS_PER_SEC: u32> Hash for Instant32<TICKS_PER_SEC>

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<const TICKS_PER_SEC: u32> Instant for Instant32<TICKS_PER_SEC>

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<const TICKS_PER_SEC: u32> Ord for Instant32<TICKS_PER_SEC>

Source§

fn cmp(&self, other: &Instant32<TICKS_PER_SEC>) -> 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<const TICKS_PER_SEC: u32> PartialEq for Instant32<TICKS_PER_SEC>

Source§

fn eq(&self, other: &Instant32<TICKS_PER_SEC>) -> 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<const TICKS_PER_SEC: u32> PartialOrd for Instant32<TICKS_PER_SEC>

Source§

fn partial_cmp(&self, other: &Instant32<TICKS_PER_SEC>) -> 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<const TICKS_PER_SEC: u32> Sub<Duration> for Instant32<TICKS_PER_SEC>

Source§

type Output = Instant32<TICKS_PER_SEC>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Duration) -> Instant32<TICKS_PER_SEC>

Performs the - operation. Read more
Source§

impl<const TICKS_PER_SEC: u32> Sub for Instant32<TICKS_PER_SEC>

Source§

fn sub(self, other: Instant32<TICKS_PER_SEC>) -> 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<const TICKS_PER_SEC: u32> SubAssign<Duration> for Instant32<TICKS_PER_SEC>

Source§

fn sub_assign(&mut self, other: Duration)

Performs the -= operation. Read more
Source§

impl<const TICKS_PER_SEC: u32> Copy for Instant32<TICKS_PER_SEC>

Source§

impl<const TICKS_PER_SEC: u32> Eq for Instant32<TICKS_PER_SEC>

Source§

impl<const TICKS_PER_SEC: u32> StructuralPartialEq for Instant32<TICKS_PER_SEC>

Auto Trait Implementations§

§

impl<const TICKS_PER_SEC: u32> Freeze for Instant32<TICKS_PER_SEC>

§

impl<const TICKS_PER_SEC: u32> RefUnwindSafe for Instant32<TICKS_PER_SEC>

§

impl<const TICKS_PER_SEC: u32> Send for Instant32<TICKS_PER_SEC>

§

impl<const TICKS_PER_SEC: u32> Sync for Instant32<TICKS_PER_SEC>

§

impl<const TICKS_PER_SEC: u32> Unpin for Instant32<TICKS_PER_SEC>

§

impl<const TICKS_PER_SEC: u32> UnwindSafe for Instant32<TICKS_PER_SEC>

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.