pub struct Timestamp { /* private fields */ }Expand description
A timestamp in a track’s timescale (units per second), or a scale-less track’s group key.
All timestamps within a track are relative, so zero for one track is not zero for another.
A scaled value is at most 2^61 - 1, the most every moq-lite version can encode as a
group’s first frame, while an unscaled key spans every group key; the scale is carried alongside so frames from different
sources can be compared and converted without lossy detours through a single fixed scale.
A scale-less track has no time: each frame’s timestamp is Self::unscaled, whose value
is its group’s key. Anything that needs a scale (Self::convert, as_secs, as_millis,
and so on) is TimeOverflow on one, and it orders against no scaled timestamp.
§An instant, not a number
A Timestamp is a point in time (like std::time::Instant), not a scalar, so it has no
arithmetic operators: adding two instants is meaningless, and a scale mismatch can’t be a
silent panic. Use Self::checked_add / Self::checked_sub, which require both
operands to share a scale and return TimeOverflow otherwise. To combine timestamps
from different scales, Self::convert one to the other’s scale first.
§Equality vs ordering
These two intentionally disagree, so pick the one you mean:
Eq/Hashare structural (field-wise):from_secs(1) != from_millis(1000), because they encode as different(value, scale)pairs on the wire. Two timestamps are equal only when both their value and scale match.PartialOrdis temporal: it cross-multiplies scales, sofrom_millis(1000)orders afterfrom_millis(999)andfrom_secs(1)slots in between. When a cross-scale comparison is otherwise a tie, it breaks by(scale, value)to stay consistent withEq. A scaled and an unscaled timestamp don’t compare:partial_cmpisNone, so<and>are both false. There is noOrd.
So from_secs(1).partial_cmp(&from_millis(1000)) is not Equal, and neither is == true.
If you want “same instant regardless of encoding”, compare after a Self::convert to a
common scale.
Implementations§
Source§impl Timestamp
impl Timestamp
Sourcepub const ZERO: Self
pub const ZERO: Self
The zero timestamp: value 0 at Timescale::SECOND.
The scale is not incidental. Equality and ordering are scale-aware (see the type
docs), so this is not interchangeable with 0 at another scale; use
Self::is_zero to test a zero value regardless of scale. In particular, don’t
seed a .max() accumulator with this: a later value at a finer scale would lose
the tie-break. Reach for Option<Timestamp> instead.
Sourcepub const fn new(value: u64, scale: Timescale) -> Result<Self, TimeOverflow>
pub const fn new(value: u64, scale: Timescale) -> Result<Self, TimeOverflow>
Construct a timestamp directly from a raw value at the given scale.
Returns TimeOverflow if value exceeds 2^61 - 1.
Sourcepub const fn unscaled(key: u64) -> Self
pub const fn unscaled(key: u64) -> Self
A scale-less track’s timestamp: key is its group’s key, with no time.
Sourcepub fn from_scale(
value: u64,
units_per_second: u64,
) -> Result<Self, TimeOverflow>
pub fn from_scale( value: u64, units_per_second: u64, ) -> Result<Self, TimeOverflow>
Construct a timestamp from a raw value and a units_per_second scale.
Returns TimeOverflow if the scale is zero or the value is out of 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 at Timescale::SECOND.
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 at Timescale::MILLI.
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 at Timescale::MICRO.
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 at Timescale::NANO.
Sourcepub const fn value(self) -> u64
pub const fn value(self) -> u64
The raw value in the timestamp’s own scale, or the group key when unscaled.
Sourcepub const fn scale(self) -> Option<Timescale>
pub const fn scale(self) -> Option<Timescale>
The scale (units per second) attached to this timestamp, or None when unscaled.
Sourcepub const fn convert(self, new_scale: Timescale) -> Result<Self, TimeOverflow>
pub const fn convert(self, new_scale: Timescale) -> Result<Self, TimeOverflow>
Re-express this timestamp at a new scale. Returns TimeOverflow if it is unscaled,
or if the new value would exceed 2^61 - 1.
Sourcepub const fn as_scale(self, target: Timescale) -> Result<u128, TimeOverflow>
pub const fn as_scale(self, target: Timescale) -> Result<u128, TimeOverflow>
The value re-expressed at target as a u128, or TimeOverflow when unscaled.
Sourcepub const fn as_secs(self) -> Result<u64, TimeOverflow>
pub const fn as_secs(self) -> Result<u64, TimeOverflow>
The value re-expressed in seconds, or TimeOverflow when unscaled.
Sourcepub const fn as_millis(self) -> Result<u128, TimeOverflow>
pub const fn as_millis(self) -> Result<u128, TimeOverflow>
The value re-expressed in milliseconds, or TimeOverflow when unscaled.
Sourcepub const fn as_micros(self) -> Result<u128, TimeOverflow>
pub const fn as_micros(self) -> Result<u128, TimeOverflow>
The value re-expressed in microseconds, or TimeOverflow when unscaled.
Sourcepub const fn as_nanos(self) -> Result<u128, TimeOverflow>
pub const fn as_nanos(self) -> Result<u128, TimeOverflow>
The value re-expressed in nanoseconds, or TimeOverflow when unscaled.
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. Returns TimeOverflow if the sum exceeds 2^61 - 1 or
if the scales differ.
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. Returns TimeOverflow if rhs > self or if the
scales differ.
Trait Implementations§
impl Copy for Timestamp
impl Eq for Timestamp
Source§impl PartialOrd for Timestamp
impl PartialOrd for Timestamp
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Temporal comparison, normalizing across scales (see the type-level docs for how
this relates to structural Eq).
- Equal scales, or two unscaled keys, compare raw values directly.
- Otherwise cross-multiplies in 128-bit so e.g.
1s > 2msorders correctly. - A would-be cross-scale tie (e.g.
from_secs(1)vsfrom_millis(1000)) breaks by(scale, value), keeping the order consistent with the field-wiseEq/Hash. - A scaled and an unscaled timestamp are
None.
impl StructuralPartialEq for Timestamp
Auto Trait Implementations§
impl Freeze for Timestamp
impl RefUnwindSafe for Timestamp
impl Send for Timestamp
impl Sync for Timestamp
impl Unpin for Timestamp
impl UnsafeUnpin for Timestamp
impl UnwindSafe for Timestamp
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§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.