unsafe 😱

This commit is contained in:
Quaternions 2024-07-31 15:48:41 -07:00
parent 3ef8ddb66a
commit 91608d5100

View File

@ -79,15 +79,22 @@ impl Run{
} }
} }
pub fn finish(&mut self,time:Time)->Result<(),Error>{ pub fn finish(&mut self,time:Time)->Result<(),Error>{
match &self.state{ //code based on std::mem::replace
RunState::Created=>Err(Error::NotStarted), let dest=&mut self.state;
RunState::Started{timer}=>{ //let's ignore ownership rules for a moment
self.state=RunState::Finished{ unsafe{
timer:TimerFixed::<Realtime,Paused>::new(time,timer.time(time)), let state=std::ptr::read(dest);
}; match state{
Ok(()) RunState::Created=>Err(Error::NotStarted),
}, RunState::Started{timer}=>{
RunState::Finished{..}=>Err(Error::AlreadyFinished), let src=RunState::Finished{
timer:timer.pause(time),
};
std::ptr::write(dest,src);
Ok(())
},
RunState::Finished{..}=>Err(Error::AlreadyFinished),
}
} }
} }
pub fn flag(&mut self,flag_reason:FlagReason){ pub fn flag(&mut self,flag_reason:FlagReason){