#[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
impl Duration
Sourcepub const fn as_nanos(&self) -> u128
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.
Sourcepub const fn from_millis(ms: u64) -> Self
pub const fn from_millis(ms: u64) -> Self
A wall-clock duration of ms whole milliseconds.
Sourcepub const fn from_ticks(ticks: u64) -> Self
pub const fn from_ticks(ticks: u64) -> Self
A duration of ticks engine frames — the clockless unit, and what the
CSS t unit becomes.
Sourcepub const fn as_ticks(&self) -> u64
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”.
Sourcepub const fn as_millis_u64(&self) -> u64
pub const fn as_millis_u64(&self) -> u64
This duration in whole milliseconds, truncating toward zero and
saturating at u64::MAX rather than wrapping.
Sourcepub fn div(&self, other: &Self) -> f32
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.
Sourcepub const fn greater_than(&self, other: &Self) -> bool
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:
- Cross-unit comparisons now answer, instead of always
false. - On
no_stdtheSystem/Systemarm used to be hardcodedfalse(there was noStdDurationto defer to); it now compares properly. - A denormalised
SystemTimeDiffwhosesecs + nanos/1e9overflowsu64used to panic insideStdDuration::new;u128nanoseconds cannot overflow.
Sourcepub const fn smaller_than(&self, other: &Self) -> bool
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§
impl Copy for Duration
impl Eq for Duration
Source§impl From<CssDuration> for Duration
Bridge from the CSS-level duration to the engine-level one, preserving the
unit.
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
fn from(d: CssDuration) -> Self
Source§impl From<Duration> for Duration
Available on crate feature std only.
impl From<Duration> for Duration
std only.