constructors and destructors for serialization

This commit is contained in:
Quaternions 2024-08-01 08:41:50 -07:00
parent 540e4a25fb
commit d37ea33b1e

View File

@ -26,6 +26,11 @@ impl PauseState for Unpaused{
pub struct Realtime{ pub struct Realtime{
offset:Time, offset:Time,
} }
impl Realtime{
const fn new(offset:Time)->Self{
Self{offset}
}
}
#[derive(Clone,Copy,Debug)] #[derive(Clone,Copy,Debug)]
pub struct Scaled{ pub struct Scaled{
@ -33,6 +38,9 @@ pub struct Scaled{
offset:Time, offset:Time,
} }
impl Scaled{ impl Scaled{
const fn new(scale:Ratio64,offset:Time)->Self{
Self{scale,offset}
}
const fn with_scale(scale:Ratio64)->Self{ const fn with_scale(scale:Ratio64)->Self{
Self{scale,offset:Time::ZERO} Self{scale,offset:Time::ZERO}
} }
@ -149,6 +157,15 @@ impl<T:TimerState,P:PauseState> TimerFixed<T,P>{
timer.set_time(time,new_time); timer.set_time(time,new_time);
timer timer
} }
pub fn from_state(state:T)->Self{
Self{
state,
_paused:P::new(),
}
}
pub fn into_state(self)->T{
self.state
}
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(),
@ -182,6 +199,18 @@ pub enum Timer<T:TimerState>{
Unpaused(TimerFixed<T,Unpaused>), Unpaused(TimerFixed<T,Unpaused>),
} }
impl<T:TimerState> Timer<T>{ impl<T:TimerState> Timer<T>{
pub fn from_state(state:T,paused:bool)->Self{
match paused{
true=>Self::Paused(TimerFixed::from_state(state)),
false=>Self::Unpaused(TimerFixed::from_state(state)),
}
}
pub fn into_state(self)->(T,bool){
match self{
Self::Paused(timer)=>(timer.into_state(),true),
Self::Unpaused(timer)=>(timer.into_state(),false),
}
}
pub fn paused(time:Time,new_time:Time)->Self{ pub fn paused(time:Time,new_time:Time)->Self{
Self::Paused(TimerFixed::new(time,new_time)) Self::Paused(TimerFixed::new(time,new_time))
} }