1use crate::AppState;
2use anyhow::Result;
3use serde::{Deserialize, Serialize};
4
5pub type CurrentTime = u64; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub struct Clock {
9 current_time: CurrentTime,
10}
11
12impl Default for Clock {
13 fn default() -> Self {
14 Self { current_time: 0 }
15 }
16}
17
18impl AppState for Clock {
19 }
21
22impl Clock {
23 pub fn current_time(&self) -> CurrentTime {
24 self.current_time
25 }
26
27 pub fn advance_by(&mut self, dt: CurrentTime) -> Result<()> {
30 if dt == 0 {
32 return Ok(());
33 }
34 self.current_time = self
36 .current_time
37 .checked_add(dt)
38 .ok_or_else(|| anyhow::anyhow!("Clock overflow"))?;
39 Ok(())
40 }
41
42 pub fn set_to(&mut self, new_time: CurrentTime) -> Result<()> {
45 if new_time < self.current_time {
46 anyhow::bail!("Cannot set clock to a time before current time (time regression).");
47 }
48 self.current_time = new_time;
49 Ok(())
50 }
51}