From 3fd6a1612459e8049953e19125e9bed82ed36dfd Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 31 Jul 2024 14:35:30 -0700 Subject: [PATCH] done --- src/run.rs | 54 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/run.rs b/src/run.rs index 76d5897..56ac491 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,4 +1,4 @@ -use crate::timer::{Timer,Scaled,Error as TimerError}; +use crate::timer::{TimerFixed,Realtime,Paused,Unpaused}; use crate::integer::Time; pub enum InvalidationReason{ @@ -19,9 +19,9 @@ impl std::fmt::Display for Error{ impl std::error::Error for Error{} enum RunState{ - Created{timer:Timer}, - Started{timer:Timer}, - Finished{timer:Timer}, + Created, + Started{timer:TimerFixed}, + Finished{timer:TimerFixed}, } pub struct Run{ @@ -33,38 +33,40 @@ pub struct Run{ impl Run{ pub fn new(created:Time)->Self{ Self{ - timer:Timer::scaled_paused( - created, - Time::ZERO, - crate::integer::Ratio64::ONE, - ), invalidated:None, created, - started:None, - finished:None, + state:RunState::Created, + } + } + pub fn time(&self,time:Time)->Time{ + match &self.state{ + RunState::Created=>Time::ZERO, + RunState::Started{timer}=>timer.time(time), + RunState::Finished{timer}=>timer.time(time), } } pub fn start(&mut self,time:Time)->Result<(),Error>{ - match self.started{ - Some(_)=>Err(Error::AlreadyStarted), - None=>{ - self.started=Some(time); - self.timer.unpause(time).map_err(Error::Timer)?; + match &self.state{ + RunState::Created=>{ + self.state=RunState::Started{ + timer:TimerFixed::::new(time,Time::ZERO), + }; Ok(()) - } + }, + RunState::Started{..}=>Err(Error::AlreadyStarted), + RunState::Finished{..}=>Err(Error::AlreadyFinished), } } pub fn finish(&mut self,time:Time)->Result<(),Error>{ - if self.started.is_none(){ - return Err(Error::NotStarted); - } - match self.finished{ - Some(_)=>Err(Error::AlreadyFinished), - None=>{ - self.finished=Some(time); - self.timer.pause(time).map_err(Error::Timer)?; + match &self.state{ + RunState::Created=>Err(Error::NotStarted), + RunState::Started{timer}=>{ + self.state=RunState::Finished{ + timer:TimerFixed::::new(time,timer.time(time)), + }; Ok(()) - } + }, + RunState::Finished{..}=>Err(Error::AlreadyFinished), } } }