Struct SimulationTimer

pub struct SimulationTimer { /* private fields */ }
Expand description

A reusable, allocation-free timer driven only by supplied elapsed durations.

Repeating timers retain elapsed % interval, including when the catch-up limit skips expirations. They never reset the remainder to zero merely because they expired. Each advance performs constant work independent of the number of elapsed intervals; callers can bound their own work by iterating only over TimerReport::expirations. No overdue work is queued internally.

One-shot timers clamp elapsed time at the interval and discard overshoot. Both modes start unpaused with zero elapsed time. Use Self::reset to reuse a timer and Self::set_paused to pause it without losing partial progress. Configuration is immutable; replace the timer to change its interval or mode.

Implementations§

§

impl SimulationTimer

pub fn once(interval: Duration) -> Result<SimulationTimer, TimerError>

Create a one-shot countdown. Zero duration returns TimerError::ZeroInterval.

Expiration is an edge in the report; Self::is_finished remains true on subsequent calls. Overshoot cannot trigger another expiration.

use ferravex_engine::time::{SimulationTimer, TimerMode};
use std::time::Duration;

let mut countdown = SimulationTimer::once(Duration::from_secs(3))?;
assert_eq!(countdown.mode(), TimerMode::Once);
assert_eq!(countdown.advance(Duration::from_secs(2))?.expirations, 0);
assert_eq!(countdown.remaining(), Duration::from_secs(1));
assert_eq!(countdown.advance(Duration::from_secs(2))?.expirations, 1);
assert!(countdown.is_finished());
assert_eq!(countdown.elapsed(), countdown.interval());
assert_eq!(countdown.advance(Duration::MAX)?.expirations, 0);
countdown.reset();
assert!(!countdown.is_finished());

pub fn repeating( interval: Duration, max_expirations: u32, ) -> Result<SimulationTimer, TimerError>

Create a repeating timer with a nonzero interval and per-call report limit.

Zero interval returns TimerError::ZeroInterval before checking the limit; a zero limit returns TimerError::ZeroCatchUpLimit. All nonzero Duration intervals and u32 limits are supported.

use ferravex_engine::time::{SimulationTimer, TimerMode, TimerReport};
use std::time::Duration;

let mut pulse = SimulationTimer::repeating(Duration::from_millis(100), 2)?;
assert_eq!(pulse.mode(), TimerMode::Repeating);
assert_eq!(pulse.max_expirations(), 2);
assert_eq!(pulse.advance(Duration::from_millis(550))?, TimerReport {
    expirations: 2, skipped: 3,
});
assert_eq!(pulse.elapsed(), Duration::from_millis(50));
assert_eq!(pulse.remaining(), Duration::from_millis(50));
assert_eq!(pulse.advance(Duration::ZERO)?, TimerReport::default());
assert_eq!(pulse.advance(Duration::from_millis(50))?.expirations, 1);
assert!(!pulse.is_finished());

pub fn advance(&mut self, delta: Duration) -> Result<TimerReport, TimerError>

Consume elapsed simulation time and report expirations for this call only.

Supply the duration of an executed simulation update, or explicitly choose a TimeStep delta for a frame-driven system. No scale is applied here. Paused or finished timers ignore the delta and return an empty report. A zero delta never expires a timer or drains previously skipped work.

A repeating timer returns TimerError::ElapsedOverflow if its remainder plus delta exceeds Duration::MAX, leaving all state unchanged. One-shot timers compare the delta to their remaining duration before addition, so even Duration::MAX can finish them without overflowing.

pub fn interval(&self) -> Duration

The nonzero duration of each interval.

pub fn mode(&self) -> TimerMode

Whether this timer expires once or repeatedly.

pub fn max_expirations(&self) -> u32

Maximum reported expirations per advance; always one for a one-shot timer.

pub fn elapsed(&self) -> Duration

Progress in the current interval; equal to the interval for a finished one-shot timer and strictly less than the interval for a repeating timer.

pub fn remaining(&self) -> Duration

Time until the next expiration, or zero for a finished one-shot timer. Immediately after a repeating expiration this is the full interval.

pub fn is_finished(&self) -> bool

Whether this one-shot timer has expired. Always false for repeating timers.

pub fn is_paused(&self) -> bool

Whether local pause currently discards supplied deltas.

pub fn set_paused(&mut self, paused: bool)

Pause or resume without changing elapsed time or expiration state. Paused deltas are discarded, not saved for catch-up on resume.

use ferravex_engine::time::SimulationTimer;
use std::time::Duration;

let mut timer = SimulationTimer::once(Duration::from_secs(2))?;
timer.advance(Duration::from_secs(1))?;
timer.set_paused(true);
assert!(timer.is_paused());
assert_eq!(timer.advance(Duration::MAX)?.expirations, 0);
assert_eq!(timer.remaining(), Duration::from_secs(1));
timer.reset(); // Clears progress, but deliberately preserves pause.
assert!(timer.is_paused());
timer.set_paused(false);
assert_eq!(timer.advance(Duration::from_secs(2))?.expirations, 1);

pub fn reset(&mut self)

Clear elapsed time and rearm a one-shot timer, preserving configuration and pause. Reset this alongside its owning game state on restart.

Trait Implementations§

§

impl Clone for SimulationTimer

§

fn clone(&self) -> SimulationTimer

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for SimulationTimer

§

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

Formats the value using the given formatter. Read more
§

impl PartialEq for SimulationTimer

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Eq for SimulationTimer

§

impl StructuralPartialEq for SimulationTimer

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, 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, 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.