Compare commits

...

2 Commits
master ... runs

Author SHA1 Message Date
db4ba34a42 nope, run needs timers with typestate 2024-07-31 13:18:53 -07:00
56ccc4b58b wip: run 2024-07-31 13:16:16 -07:00
2 changed files with 71 additions and 0 deletions

View File

@ -1,5 +1,6 @@
pub mod bvh;
pub mod map;
pub mod run;
pub mod aabb;
pub mod model;
pub mod zeroes;

70
src/run.rs Normal file
View File

@ -0,0 +1,70 @@
use crate::timer::{Timer,Scaled,Error as TimerError};
use crate::integer::Time;
pub enum InvalidationReason{
}
#[derive(Debug)]
pub enum Error{
AlreadyStarted,
NotStarted,
AlreadyFinished,
}
impl std::fmt::Display for Error{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for Error{}
enum RunState{
Created{timer:Timer<Paused>},
Started{timer:Timer<Unpaused>},
Finished{timer:Timer<Paused>},
}
pub struct Run{
invalidated:Option<InvalidationReason>,
created:Time,
state:RunState,
}
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,
}
}
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)?;
Ok(())
}
}
}
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)?;
Ok(())
}
}
}
}