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>
impl<const NOM: u32, const DENOM: u32> Instant<u32, NOM, DENOM>
sourcepub const fn from_ticks(ticks: u32) -> Self
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);
sourcepub const fn ticks(&self) -> u32
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);
sourcepub const fn const_cmp(self, other: Self) -> Ordering
pub const fn const_cmp(self, other: Self) -> Ordering
Const comparison of Instant
s.
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);
sourcepub const fn duration_since_epoch(self) -> Duration<u32, NOM, DENOM>
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);
sourcepub const fn checked_duration_since(
self,
other: Self,
) -> Option<Duration<u32, NOM, DENOM>>
pub const fn checked_duration_since( self, other: Self, ) -> Option<Duration<u32, NOM, DENOM>>
Duration between Instant
s.
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);
sourcepub const fn checked_sub_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Duration<u32, O_NOM, O_DENOM>,
) -> Option<Self>
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);
sourcepub const fn checked_add_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Duration<u32, O_NOM, O_DENOM>,
) -> Option<Self>
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>
impl<const NOM: u32, const DENOM: u32> Instant<u64, NOM, DENOM>
sourcepub const fn from_ticks(ticks: u64) -> Self
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);
sourcepub const fn ticks(&self) -> u64
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);
sourcepub const fn const_cmp(self, other: Self) -> Ordering
pub const fn const_cmp(self, other: Self) -> Ordering
Const comparison of Instant
s.
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);
sourcepub const fn duration_since_epoch(self) -> Duration<u64, NOM, DENOM>
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);
sourcepub const fn checked_duration_since(
self,
other: Self,
) -> Option<Duration<u64, NOM, DENOM>>
pub const fn checked_duration_since( self, other: Self, ) -> Option<Duration<u64, NOM, DENOM>>
Duration between Instant
s.
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);
sourcepub const fn checked_sub_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Duration<u64, O_NOM, O_DENOM>,
) -> Option<Self>
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);
sourcepub const fn checked_add_duration<const O_NOM: u32, const O_DENOM: u32>(
self,
other: Duration<u64, O_NOM, O_DENOM>,
) -> Option<Self>
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>
impl<const NOM: u32, const DENOM: u32> Add<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> Add<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Add<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> Add<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Add<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> AddAssign<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> Ord for Instant<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Ord for Instant<u32, NOM, DENOM>
source§fn cmp(&self, other: &Self) -> Ordering
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<const NOM: u32, const DENOM: u32> Ord for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Ord for Instant<u64, NOM, DENOM>
source§fn cmp(&self, other: &Self) -> Ordering
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<const NOM: u32, const DENOM: u32> PartialOrd for Instant<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> PartialOrd for Instant<u32, NOM, DENOM>
source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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.
source§impl<const NOM: u32, const DENOM: u32> PartialOrd for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> PartialOrd for Instant<u64, NOM, DENOM>
source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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.
source§impl<const NOM: u32, const DENOM: u32> Sub<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Sub<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> Sub<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Sub<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> Sub<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Sub<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u32, NOM, DENOM>> for Instant<u32, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u32, NOM, DENOM>> for Instant<u64, NOM, DENOM>
source§impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> SubAssign<Duration<u64, NOM, DENOM>> for Instant<u64, NOM, DENOM>
impl<T: Copy, const NOM: u32, const DENOM: u32> Copy for Instant<T, NOM, DENOM>
impl<const NOM: u32, const DENOM: u32> Eq for Instant<u32, NOM, DENOM>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)