From d37ea33b1ecf48009307d4b468225655c9b39e09 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 1 Aug 2024 08:41:50 -0700 Subject: [PATCH] constructors and destructors for serialization --- src/timer.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/timer.rs b/src/timer.rs index c49652e..1255d48 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -26,6 +26,11 @@ impl PauseState for Unpaused{ pub struct Realtime{ offset:Time, } +impl Realtime{ + const fn new(offset:Time)->Self{ + Self{offset} + } +} #[derive(Clone,Copy,Debug)] pub struct Scaled{ @@ -33,6 +38,9 @@ pub struct Scaled{ offset:Time, } impl Scaled{ + const fn new(scale:Ratio64,offset:Time)->Self{ + Self{scale,offset} + } const fn with_scale(scale:Ratio64)->Self{ Self{scale,offset:Time::ZERO} } @@ -149,6 +157,15 @@ impl TimerFixed{ timer.set_time(time,new_time); 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{ match P::IS_PAUSED{ true=>self.state.get_offset(), @@ -182,6 +199,18 @@ pub enum Timer{ Unpaused(TimerFixed), } impl Timer{ + 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{ Self::Paused(TimerFixed::new(time,new_time)) }