Skip to main content

Timestamp

Struct Timestamp 

Source
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 / Hash are 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.
  • PartialOrd is temporal: it cross-multiplies scales, so from_millis(1000) orders after from_millis(999) and from_secs(1) slots in between. When a cross-scale comparison is otherwise a tie, it breaks by (scale, value) to stay consistent with Eq. A scaled and an unscaled timestamp don’t compare: partial_cmp is None, so < and > are both false. There is no Ord.

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

Source

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.

Source

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.

Source

pub const fn unscaled(key: u64) -> Self

A scale-less track’s timestamp: key is its group’s key, with no time.

Source

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.

Source

pub const fn from_secs(seconds: u64) -> Result<Self, TimeOverflow>

Convert a number of seconds to a timestamp at Timescale::SECOND.

Source

pub const fn from_millis(millis: u64) -> Result<Self, TimeOverflow>

Convert a number of milliseconds to a timestamp at Timescale::MILLI.

Source

pub const fn from_micros(micros: u64) -> Result<Self, TimeOverflow>

Convert a number of microseconds to a timestamp at Timescale::MICRO.

Source

pub const fn from_nanos(nanos: u64) -> Result<Self, TimeOverflow>

Convert a number of nanoseconds to a timestamp at Timescale::NANO.

Source

pub const fn value(self) -> u64

The raw value in the timestamp’s own scale, or the group key when unscaled.

Source

pub const fn scale(self) -> Option<Timescale>

The scale (units per second) attached to this timestamp, or None when unscaled.

Source

pub const fn is_zero(self) -> bool

Whether the raw value is zero. Does not consider scale.

Source

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.

Source

pub const fn as_scale(self, target: Timescale) -> Result<u128, TimeOverflow>

The value re-expressed at target as a u128, or TimeOverflow when unscaled.

Source

pub const fn as_secs(self) -> Result<u64, TimeOverflow>

The value re-expressed in seconds, or TimeOverflow when unscaled.

Source

pub const fn as_millis(self) -> Result<u128, TimeOverflow>

The value re-expressed in milliseconds, or TimeOverflow when unscaled.

Source

pub const fn as_micros(self) -> Result<u128, TimeOverflow>

The value re-expressed in microseconds, or TimeOverflow when unscaled.

Source

pub const fn as_nanos(self) -> Result<u128, TimeOverflow>

The value re-expressed in nanoseconds, or TimeOverflow when unscaled.

Source

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.

Source

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§

Source§

impl Clone for Timestamp

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Timestamp

Source§

impl Debug for Timestamp

Source§

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

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

impl Eq for Timestamp

Source§

impl From<Instant> for Timestamp

Source§

fn from(instant: Instant) -> Self

Convert an Instant into a millisecond-scale timestamp, measured from the model clock’s anchor.

One-way only: there is no inverse, since the anchor is jittered to keep a Timestamp from being read back as a clock.

Source§

impl Hash for Timestamp

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 PartialEq for Timestamp

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Timestamp

Source§

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 > 2ms orders correctly.
  • A would-be cross-scale tie (e.g. from_secs(1) vs from_millis(1000)) breaks by (scale, value), keeping the order consistent with the field-wise Eq/Hash.
  • A scaled and an unscaled timestamp are None.
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 StructuralPartialEq for Timestamp

Source§

impl TryFrom<Duration> for Timestamp

Source§

fn try_from(duration: Duration) -> Result<Self, Self::Error>

Convert a std::time::Duration into a nanosecond-scale timestamp.

Source§

type Error = TimeOverflow

The type returned in the event of a conversion error.
Source§

impl TryFrom<Timestamp> for Duration

Source§

fn try_from(time: Timestamp) -> Result<Self, Self::Error>

The time since the track’s zero, or TimeOverflow when unscaled.

Source§

type Error = TimeOverflow

The type returned in the event of a conversion error.

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more