change api

This commit is contained in:
Quaternions 2024-07-31 19:06:54 -07:00
parent a98b71f0da
commit e55be91d24

View File

@ -26,11 +26,6 @@ impl PauseState for Unpaused{
pub struct Realtime{ pub struct Realtime{
offset:Time, offset:Time,
} }
impl Realtime{
const fn identity()->Self{
Self{offset:Time::ZERO}
}
}
#[derive(Clone,Copy,Debug)] #[derive(Clone,Copy,Debug)]
pub struct Scaled{ pub struct Scaled{
@ -38,9 +33,6 @@ pub struct Scaled{
offset:Time, offset:Time,
} }
impl Scaled{ impl Scaled{
// const fn identity()->Self{
// Self{scale:Ratio64::ONE,offset:Time::ZERO}
// }
const fn with_scale(scale:Ratio64)->Self{ const fn with_scale(scale:Ratio64)->Self{
Self{scale,offset:Time::ZERO} Self{scale,offset:Time::ZERO}
} }
@ -58,12 +50,16 @@ impl Scaled{
} }
pub trait TimerState:Copy+std::fmt::Debug{ pub trait TimerState:Copy+std::fmt::Debug{
fn identity()->Self;
fn get_time(&self,time:Time)->Time; fn get_time(&self,time:Time)->Time;
fn set_time(&mut self,time:Time,new_time:Time); fn set_time(&mut self,time:Time,new_time:Time);
fn get_offset(&self)->Time; fn get_offset(&self)->Time;
fn set_offset(&mut self,offset:Time); fn set_offset(&mut self,offset:Time);
} }
impl TimerState for Realtime{ impl TimerState for Realtime{
fn identity()->Self{
Self{offset:Time::ZERO}
}
fn get_time(&self,time:Time)->Time{ fn get_time(&self,time:Time)->Time{
time+self.offset time+self.offset
} }
@ -78,6 +74,9 @@ impl TimerState for Realtime{
} }
} }
impl TimerState for Scaled{ impl TimerState for Scaled{
fn identity()->Self{
Self{scale:Ratio64::ONE,offset:Time::ZERO}
}
fn get_time(&self,time:Time)->Time{ fn get_time(&self,time:Time)->Time{
self.scale(time)+self.offset self.scale(time)+self.offset
} }
@ -98,19 +97,9 @@ pub struct TimerFixed<T:TimerState,P:PauseState>{
_paused:P, _paused:P,
} }
//constructors are generic across PauseState //scaled timer methods are generic across PauseState
impl<P:PauseState> TimerFixed<Realtime,P>{
pub fn new(time:Time,new_time:Time)->Self{
let mut timer=Self{
state:Realtime::identity(),
_paused:P::new(),
};
timer.set_time(time,new_time);
timer
}
}
impl<P:PauseState> TimerFixed<Scaled,P>{ impl<P:PauseState> TimerFixed<Scaled,P>{
pub fn new(time:Time,new_time:Time,scale:Ratio64)->Self{ pub fn scaled(time:Time,new_time:Time,scale:Ratio64)->Self{
let mut timer=Self{ let mut timer=Self{
state:Scaled::with_scale(scale), state:Scaled::with_scale(scale),
_paused:P::new(), _paused:P::new(),
@ -118,9 +107,6 @@ impl<P:PauseState> TimerFixed<Scaled,P>{
timer.set_time(time,new_time); timer.set_time(time,new_time);
timer timer
} }
pub fn unit(time:Time,new_time:Time)->Self{
Self::new(time,new_time,Ratio64::ONE)
}
pub const fn get_scale(&self)->Ratio64{ pub const fn get_scale(&self)->Ratio64{
self.state.get_scale() self.state.get_scale()
} }
@ -153,8 +139,16 @@ impl<T:TimerState> TimerFixed<T,Unpaused>{
} }
} }
//time queries are generic across both //the new constructor and time queries are generic across both
impl<T:TimerState,P:PauseState> TimerFixed<T,P>{ impl<T:TimerState,P:PauseState> TimerFixed<T,P>{
pub fn new(time:Time,new_time:Time)->Self{
let mut timer=Self{
state:T::identity(),
_paused:P::new(),
};
timer.set_time(time,new_time);
timer
}
pub fn time(&self,time:Time)->Time{ pub fn time(&self,time:Time)->Time{
match P::IS_PAUSED{ match P::IS_PAUSED{
true=>self.state.get_offset(), true=>self.state.get_offset(),