Skip to main content

Duration

Enum Duration 

Source
#[repr(C, u8)]
pub enum Duration { System(SystemTimeDiff), Tick(SystemTickDiff), }
Expand description

A span of time, either from the system clock or as tick difference.

Mirrors Instant variants - System durations work with System instants, Tick durations work with Tick instants.

Variants§

§

System(SystemTimeDiff)

System duration from std::time::Duration (requires “std” feature)

§

Tick(SystemTickDiff)

Tick-based duration for embedded systems

Implementations§

Source§

impl Duration

Source

pub const fn as_nanos(&self) -> u128

This duration on ONE canonical scale, in nanoseconds — the common ground on which a Tick span and a System span can be compared.

u128 because a System duration holds up to u64::MAX seconds (~1.8e28 ns), which does not fit u64. The tick conversion multiplies before it divides so whole seconds stay exact: 60t is 1_000_000_000ns, not 60 * 16_666_666 = 999_999_960ns.

Note this also normalises a DENORMALISED SystemTimeDiff (nanos past 1e9) the same way std::time::Duration::new would — except it cannot panic on overflow while doing it.

Source

pub const fn from_millis(ms: u64) -> Self

A wall-clock duration of ms whole milliseconds.

Source

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

A duration of ticks engine frames — the clockless unit, and what the CSS t unit becomes.

Source

pub const fn as_ticks(&self) -> u64

This duration in whole ticks (frames), truncating toward zero.

A sub-frame span is zero ticks, not one: “how many whole frames fit”, never “round up so that something happens”.

Source

pub const fn as_millis_u64(&self) -> u64

This duration in whole milliseconds, truncating toward zero and saturating at u64::MAX rather than wrapping.

Source

pub fn max() -> Self

Returns the maximum possible duration.

Source

pub fn div(&self, other: &Self) -> f32

Divides this duration by another, returning the ratio as f32.

Same-unit division goes through the unit’s own div so its exact floating-point result is unchanged. Cross-unit division falls back to the canonical nanosecond scale rather than returning 0.0 — a 0.0 ratio here means “animation is at 0% progress”, which is a frozen animation, not an error anyone would notice.

Source

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

Returns the smaller of two durations.

Source

pub const fn greater_than(&self, other: &Self) -> bool

Returns true if self > other.

Compares on the canonical nanosecond scale (Self::as_nanos), so a Tick span and a System span compare TRUTHFULLY against each other.

§Why this is not “mismatched kinds saturate to false”

It used to be. That made a unit mismatch invisible and permanent instead of loud: the engine’s interval constants are Duration::System (the cursor blink, the scrollbar fade, the tooltip delay), so the moment a clock produced Tick elapsed values every one of those comparisons answered “not yet” — forever. Nothing panicked, nothing logged, the UI simply stopped animating. That is precisely the failure a clockless unit is supposed to make catchable, so the comparison has to be total.

Three behaviours changed, all in the safe direction:

  1. Cross-unit comparisons now answer, instead of always false.
  2. On no_std the System/System arm used to be hardcoded false (there was no StdDuration to defer to); it now compares properly.
  3. A denormalised SystemTimeDiff whose secs + nanos/1e9 overflows u64 used to panic inside StdDuration::new; u128 nanoseconds cannot overflow.
Source

pub const fn smaller_than(&self, other: &Self) -> bool

Returns true if self < other.

Canonical-scale comparison; see Self::greater_than for why this is unit-aware rather than saturating to false on a unit mismatch.

Trait Implementations§

Source§

impl Clone for Duration

Source§

fn clone(&self) -> Duration

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 Duration

Source§

impl Debug for Duration

Source§

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

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

impl Display for Duration

Source§

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

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

impl Eq for Duration

Source§

impl From<CssDuration> for Duration

Bridge from the CSS-level duration to the engine-level one, preserving the unit.

This is the join that makes a CSS 5t mean five FRAMES all the way down to the timer: ms/s become Duration::System, t becomes Duration::Tick. Collapsing ticks to milliseconds here would put the wall clock back in the path and make “advance exactly 5 ticks, assert the 5th frame flipped” untestable again.

Source§

fn from(d: CssDuration) -> Self

Converts to this type from the input type.
Source§

impl From<Duration> for Duration

Available on crate feature std only.
Source§

fn from(s: StdDuration) -> Self

Converts to this type from the input type.
Source§

impl Hash for Duration

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 Ord for Duration

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

impl PartialEq for Duration

Source§

fn eq(&self, other: &Duration) -> 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 Duration

Source§

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

This method returns an ordering between self and other values if one exists. Read more
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 Duration

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GetHash for T
where T: Hash,

Source§

fn get_hash(&self) -> u64

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.