Struct fugit::Instant

source ·
pub struct Instant<T, const NOM: u32, const DENOM: u32> { /* private fields */ }
Expand description

Represents an instant in time.

The generic T can either be u32 or u64, and the const generics represent the ratio of the ticks contained within the instant: instant in seconds = NOM / DENOM * ticks

Implementations§

source§

impl<const NOM: u32, const DENOM: u32> Instant<u32, NOM, DENOM>

source

pub const fn from_ticks(ticks: u32) -> Self

Create an Instant from a ticks value.

let _i = Instant::<u32, 1, 1_000>::from_ticks(1);
source

pub const fn ticks(&self) -> u32

Extract the ticks from an Instant.

let i = Instant::<u32, 1, 1_000>::from_ticks(234);

assert_eq!(i.ticks(), 234);
source

pub const fn const_cmp(self, other: Self) -> Ordering

Const comparison of Instants.

let i1 = Instant::<u32, 1, 1_000>::from_ticks(1);
let i2 = Instant::<u32, 1, 1_000>::from_ticks(2);

assert_eq!(i1.const_cmp(i2), core::cmp::Ordering::Less);

This function takes into account that ticks might wrap around. If the absolute values of self and other differ by more than half the possible range, it is assumed that an overflow occured and the result is reversed:

let i1 = Instant::<u32, 1, 1_000>::from_ticks(u32::MAX);
let i2 = Instant::<u32, 1, 1_000>::from_ticks(1);

assert_eq!(i1.const_cmp(i2), core::cmp::Ordering::Less);
source

pub const fn duration_since_epoch(self) -> Duration<u32, NOM, DENOM>

Duration between since the start of the Instant. This assumes an instant which won’t wrap within the execution of the program.

let i = Instant::<u32, 1, 1_000>::from_ticks(11);

assert_eq!(i.duration_since_epoch().ticks(), 11);
source

pub const fn checked_duration_since( self, other: Self, ) -> Option<Duration<u32, NOM, DENOM>>

Duration between Instants.

let i1 = Instant::<u32, 1, 1_000>::from_ticks(1);
let i2 = Instant::<u32, 1, 1_000>::from_ticks(2);

assert_eq!(i1.checked_duration_since(i2), None);
assert_eq!(i2.checked_duration_since(i1).unwrap().ticks(), 1);
source

pub const fn checked_sub_duration<const O_NOM: u32, const O_DENOM: u32>( self, other: Duration<u32, O_NOM, O_DENOM>, ) -> Option<Self>

Subtract a Duration from an Instant while checking for overflow.

let i = Instant::<u32, 1, 1_000>::from_ticks(1);
let d = Duration::<u32, 1, 1_000>::from_ticks(1);

assert_eq!(i.checked_sub_duration(d).unwrap().ticks(), 0);
source

pub const fn checked_add_duration<const O_NOM: u32, const O_DENOM: u32>( self, other: Duration<u32, O_NOM, O_DENOM>, ) -> Option<Self>

Add a Duration to an Instant while checking for overflow.

let i = Instant::<u32, 1, 1_000>::from_ticks(1);
let d = Duration::<u32, 1, 1_000>::from_ticks(1);

assert_eq!(i.checked_add_duration(d).unwrap().ticks(), 2);
source§

impl<const NOM: u32, const DENOM: u32> Instant<u64, NOM, DENOM>

source

pub const fn from_ticks(ticks: u64) -> Self

Create an Instant from a ticks value.

let _i = Instant::<u64, 1, 1_000>::from_ticks(1);
source

pub const fn ticks(&self) -> u64

Extract the ticks from an Instant.

let i = Instant::<u64, 1, 1_000>::from_ticks(234);

assert_eq!(i.ticks(), 234);
source

pub const fn const_cmp(self, other: Self) -> Ordering

Const comparison of Instants.

let i1 = Instant::<u64, 1, 1_000>::from_ticks(1);
let i2 = Instant::<u64, 1, 1_000>::from_ticks(2);

assert_eq!(i1.const_cmp(i2), core::cmp::Ordering::Less);

This function takes into account that ticks might wrap around. If the absolute values of self and other differ by more than half the possible range, it is assumed that an overflow occured and the result is reversed:

let i1 = Instant::<u64, 1, 1_000>::from_ticks(u64::MAX);
let i2 = Instant::<u64, 1, 1_000>::from_ticks(1);

assert_eq!(i1.const_cmp(i2), core::cmp::Ordering::Less);
source

pub const fn duration_since_epoch(self) -> Duration<u64, NOM, DENOM>

Duration between since the start of the Instant. This assumes an instant which won’t wrap within the execution of the program.

let i = Instant::<u64, 1, 1_000>::from_ticks(11);

assert_eq!(i.duration_since_epoch().ticks(), 11);
source

pub const fn checked_duration_since( self, other: Self, ) -> Option<Duration<u64, NOM, DENOM>>

Duration between Instants.

let i1 = Instant::<u64, 1, 1_000>::from_ticks(1);
let i2 = Instant::<u64, 1, 1_000>::from_ticks(2);

assert_eq!(i1.checked_duration_since(i2), None);
assert_eq!(i2.checked_duration_since(i1).unwrap().ticks(), 1);
source

pub const fn checked_sub_duration<const O_NOM: u32, const O_DENOM: u32>( self, other: Duration<u64, O_NOM, O_DENOM>, ) -> Option<Self>

Subtract a Duration from an Instant while checking for overflow.

let i = Instant::<u64, 1, 1_000>::from_ticks(1);
let d = Duration::<u64, 1, 1_000>::from_ticks(1);

assert_eq!(i.checked_sub_duration(d).unwrap().ticks(), 0);
source

pub const fn checked_add_duration<const O_NOM: u32, const O_DENOM: u32>( self, other: Duration<u64, O_NOM, O_DENOM>, ) -> Option<Self>

Add a Duration to an Instant while checking for overflow.

let i = Instant::<u64, 1, 1_000>::from_ticks(1);
let d = Duration::<u64, 1, 1_000>::from_ticks(1);

assert_eq!(i.checked_add_duration(d).unwrap().ticks(), 2);

Trait Implementations§

source§

impl<const NOM: u32, const DENOM: u32> Add<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>

source§

type Output = Instant<u32, NOM, DENOM>

The resulting type after applying the + operator.
source§

fn add(self, other: Duration<u32, NOM, DENOM>) -> Self::Output

Performs the + operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> Add<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

type Output = Instant<u64, NOM, DENOM>

The resulting type after applying the + operator.
source§

fn add(self, other: Duration<u32, NOM, DENOM>) -> Self::Output

Performs the + operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> Add<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

type Output = Instant<u64, NOM, DENOM>

The resulting type after applying the + operator.
source§

fn add(self, other: Duration<u64, NOM, DENOM>) -> Self::Output

Performs the + operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>

source§

fn add_assign(&mut self, other: Duration<u32, NOM, DENOM>)

Performs the += operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

fn add_assign(&mut self, other: Duration<u32, NOM, DENOM>)

Performs the += operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

fn add_assign(&mut self, other: Duration<u64, NOM, DENOM>)

Performs the += operation. Read more
source§

impl<T: Clone, const NOM: u32, const DENOM: u32> Clone for Instant<T, NOM, DENOM>

source§

fn clone(&self) -> Instant<T, NOM, DENOM>

Returns a copy 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<T: Debug, const NOM: u32, const DENOM: u32> Debug for Instant<T, NOM, DENOM>

source§

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

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

impl<const NOM: u32, const DENOM: u32> Display for Instant<u32, NOM, DENOM>

source§

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

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

impl<const NOM: u32, const DENOM: u32> Display for Instant<u64, NOM, DENOM>

source§

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

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

impl<const NOM: u32, const DENOM: u32> Ord for Instant<u32, NOM, DENOM>

source§

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

This implementation deviates from the definition of Ord::cmp:

It takes into account that ticks might wrap around. If the absolute values of self and other differ by more than half the possible range, it is assumed that an overflow occured and the result is reversed.

That breaks the transitivity invariant: a < b and b < c no longer implies a < c.

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 + PartialOrd,

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

impl<const NOM: u32, const DENOM: u32> Ord for Instant<u64, NOM, DENOM>

source§

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

This implementation deviates from the definition of Ord::cmp:

It takes into account that ticks might wrap around. If the absolute values of self and other differ by more than half the possible range, it is assumed that an overflow occured and the result is reversed.

That breaks the transitivity invariant: a < b and b < c no longer implies a < c.

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 + PartialOrd,

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

impl<const NOM: u32, const DENOM: u32> PartialEq for Instant<u32, NOM, DENOM>

source§

fn eq(&self, other: &Self) -> 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 NOM: u32, const DENOM: u32> PartialEq for Instant<u64, NOM, DENOM>

source§

fn eq(&self, other: &Self) -> 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 NOM: u32, const DENOM: u32> PartialOrd for Instant<u32, NOM, DENOM>

source§

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

This implementation deviates from the definition of PartialOrd::partial_cmp:

It takes into account that ticks might wrap around. If the absolute values of self and other differ by more than half the possible range, it is assumed that an overflow occured and the result is reversed.

That breaks the transitivity invariant: a < b and b < c no longer implies a < c.

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 NOM: u32, const DENOM: u32> PartialOrd for Instant<u64, NOM, DENOM>

source§

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

This implementation deviates from the definition of PartialOrd::partial_cmp:

It takes into account that ticks might wrap around. If the absolute values of self and other differ by more than half the possible range, it is assumed that an overflow occured and the result is reversed.

That breaks the transitivity invariant: a < b and b < c no longer implies a < c.

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 NOM: u32, const DENOM: u32> Sub<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>

source§

type Output = Instant<u32, NOM, DENOM>

The resulting type after applying the - operator.
source§

fn sub(self, other: Duration<u32, NOM, DENOM>) -> Self::Output

Performs the - operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> Sub<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

type Output = Instant<u64, NOM, DENOM>

The resulting type after applying the - operator.
source§

fn sub(self, other: Duration<u32, NOM, DENOM>) -> Self::Output

Performs the - operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> Sub<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

type Output = Instant<u64, NOM, DENOM>

The resulting type after applying the - operator.
source§

fn sub(self, other: Duration<u64, NOM, DENOM>) -> Self::Output

Performs the - operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> Sub for Instant<u32, NOM, DENOM>

source§

type Output = Duration<u32, NOM, DENOM>

The resulting type after applying the - operator.
source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> Sub for Instant<u64, NOM, DENOM>

source§

type Output = Duration<u64, NOM, DENOM>

The resulting type after applying the - operator.
source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>

source§

fn sub_assign(&mut self, other: Duration<u32, NOM, DENOM>)

Performs the -= operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

fn sub_assign(&mut self, other: Duration<u32, NOM, DENOM>)

Performs the -= operation. Read more
source§

impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>

source§

fn sub_assign(&mut self, other: Duration<u64, NOM, DENOM>)

Performs the -= operation. Read more
source§

impl<T: Copy, const NOM: u32, const DENOM: u32> Copy for Instant<T, NOM, DENOM>

source§

impl<const NOM: u32, const DENOM: u32> Eq for Instant<u32, NOM, DENOM>

source§

impl<const NOM: u32, const DENOM: u32> Eq for Instant<u64, NOM, DENOM>

Auto Trait Implementations§

§

impl<T, const NOM: u32, const DENOM: u32> Freeze for Instant<T, NOM, DENOM>
where T: Freeze,

§

impl<T, const NOM: u32, const DENOM: u32> RefUnwindSafe for Instant<T, NOM, DENOM>
where T: RefUnwindSafe,

§

impl<T, const NOM: u32, const DENOM: u32> Send for Instant<T, NOM, DENOM>
where T: Send,

§

impl<T, const NOM: u32, const DENOM: u32> Sync for Instant<T, NOM, DENOM>
where T: Sync,

§

impl<T, const NOM: u32, const DENOM: u32> Unpin for Instant<T, NOM, DENOM>
where T: Unpin,

§

impl<T, const NOM: u32, const DENOM: u32> UnwindSafe for Instant<T, NOM, DENOM>
where T: UnwindSafe,

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.