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
impl SimulationTimer
pub fn once(interval: Duration) -> Result<SimulationTimer, TimerError>
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>
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>
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 max_expirations(&self) -> u32
pub fn max_expirations(&self) -> u32
Maximum reported expirations per advance; always one for a one-shot timer.
pub fn elapsed(&self) -> Duration
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
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
pub fn is_finished(&self) -> bool
Whether this one-shot timer has expired. Always false for repeating timers.
pub fn set_paused(&mut self, paused: bool)
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)
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
impl Clone for SimulationTimer
§fn clone(&self) -> SimulationTimer
fn clone(&self) -> SimulationTimer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more