pub struct Timescale<const SCALE: u64>(/* private fields */);Expand description
A timestamp representing the presentation time in a given scale. ex. 1000 for milliseconds.
All timestamps within a track are relative, so zero for one track is not zero for another. Values are constrained to fit within a QUIC VarInt (2^62) so they can be encoded and decoded easily.
This is std::time::Instant and std::time::Duration merged into one type for simplicity.
Implementations§
Source§impl<const SCALE: u64> Timescale<SCALE>
impl<const SCALE: u64> Timescale<SCALE>
Sourcepub const fn new(value: u32) -> Self
pub const fn new(value: u32) -> Self
Construct a timestamp directly from a value in this scale’s units. Infallible
because any u32 fits within the 62-bit varint range.
Sourcepub const fn new_u64(value: u64) -> Result<Self, TimeOverflow>
pub const fn new_u64(value: u64) -> Result<Self, TimeOverflow>
Construct a timestamp directly from a value in this scale’s units. Returns
TimeOverflow if value exceeds the 62-bit varint range.
Sourcepub const fn from_secs(seconds: u64) -> Result<Self, TimeOverflow>
pub const fn from_secs(seconds: u64) -> Result<Self, TimeOverflow>
Convert a number of seconds to a timestamp, returning an error if the timestamp would overflow.
Sourcepub const fn from_secs_unchecked(seconds: u64) -> Self
pub const fn from_secs_unchecked(seconds: u64) -> Self
Like Self::from_secs but panics on overflow. Intended for const
initializers where overflow indicates a bug, not a runtime condition.
Sourcepub const fn from_millis(millis: u64) -> Result<Self, TimeOverflow>
pub const fn from_millis(millis: u64) -> Result<Self, TimeOverflow>
Convert a number of milliseconds to a timestamp, returning an error if the timestamp would overflow.
Sourcepub const fn from_millis_unchecked(millis: u64) -> Self
pub const fn from_millis_unchecked(millis: u64) -> Self
Like Self::from_millis but panics on overflow.
Sourcepub const fn from_micros(micros: u64) -> Result<Self, TimeOverflow>
pub const fn from_micros(micros: u64) -> Result<Self, TimeOverflow>
Convert a number of microseconds to a timestamp, returning an error on overflow.
Sourcepub const fn from_micros_unchecked(micros: u64) -> Self
pub const fn from_micros_unchecked(micros: u64) -> Self
Like Self::from_micros but panics on overflow.
Sourcepub const fn from_nanos(nanos: u64) -> Result<Self, TimeOverflow>
pub const fn from_nanos(nanos: u64) -> Result<Self, TimeOverflow>
Convert a number of nanoseconds to a timestamp, returning an error on overflow.
Sourcepub const fn from_nanos_unchecked(nanos: u64) -> Self
pub const fn from_nanos_unchecked(nanos: u64) -> Self
Like Self::from_nanos but panics on overflow.
Sourcepub const fn from_scale(value: u64, scale: u64) -> Result<Self, TimeOverflow>
pub const fn from_scale(value: u64, scale: u64) -> Result<Self, TimeOverflow>
Construct from value measured at the given scale (units per second), rescaling
to SCALE. Returns TimeOverflow if the rescaled value exceeds 2^62.
Sourcepub const fn from_scale_u128(
value: u128,
scale: u64,
) -> Result<Self, TimeOverflow>
pub const fn from_scale_u128( value: u128, scale: u64, ) -> Result<Self, TimeOverflow>
Like Self::from_scale but accepts a u128 source value.
Sourcepub const fn from_scale_unchecked(value: u64, scale: u64) -> Self
pub const fn from_scale_unchecked(value: u64, scale: u64) -> Self
Like Self::from_scale but panics on overflow.
Sourcepub const fn as_scale(self, scale: u64) -> u128
pub const fn as_scale(self, scale: u64) -> u128
Convert this timestamp to the given scale (units per second).
Sourcepub const fn checked_add(self, rhs: Self) -> Result<Self, TimeOverflow>
pub const fn checked_add(self, rhs: Self) -> Result<Self, TimeOverflow>
Add two timestamps, returning TimeOverflow if the sum exceeds 2^62.
Sourcepub const fn checked_sub(self, rhs: Self) -> Result<Self, TimeOverflow>
pub const fn checked_sub(self, rhs: Self) -> Result<Self, TimeOverflow>
Subtract rhs from self, returning TimeOverflow if rhs > self.
Sourcepub const fn is_zero(self) -> bool
pub const fn is_zero(self) -> bool
Whether this timestamp is Self::ZERO.
Sourcepub fn now() -> Self
pub fn now() -> Self
Current time as a timestamp, derived from tokio::time::Instant::now so
it honors tokio::time::pause in tests.
Sourcepub const fn convert<const NEW_SCALE: u64>(
self,
) -> Result<Timescale<NEW_SCALE>, TimeOverflow>
pub const fn convert<const NEW_SCALE: u64>( self, ) -> Result<Timescale<NEW_SCALE>, TimeOverflow>
Convert this timestamp to a different scale.
This allows converting between different TimeScale types, for example from milliseconds to microseconds. Note that converting to a coarser scale may lose precision due to integer division.
Trait Implementations§
Source§impl<const SCALE: u64> AddAssign for Timescale<SCALE>
impl<const SCALE: u64> AddAssign for Timescale<SCALE>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<const SCALE: u64> Ord for Timescale<SCALE>
impl<const SCALE: u64> Ord for Timescale<SCALE>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const SCALE: u64> PartialEq for Timescale<SCALE>
impl<const SCALE: u64> PartialEq for Timescale<SCALE>
Source§impl<const SCALE: u64> PartialOrd for Timescale<SCALE>
impl<const SCALE: u64> PartialOrd for Timescale<SCALE>
Source§impl<const SCALE: u64> SubAssign for Timescale<SCALE>
impl<const SCALE: u64> SubAssign for Timescale<SCALE>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more