From 91608d51004d05d489f1093b6c90f2c0abd16ace Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 31 Jul 2024 15:48:41 -0700 Subject: [PATCH] =?UTF-8?q?unsafe=20=F0=9F=98=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/run.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/run.rs b/src/run.rs index 6fcfb2e..bf5ba8d 100644 --- a/src/run.rs +++ b/src/run.rs @@ -79,15 +79,22 @@ impl Run{ } } pub fn finish(&mut self,time:Time)->Result<(),Error>{ - match &self.state{ - RunState::Created=>Err(Error::NotStarted), - RunState::Started{timer}=>{ - self.state=RunState::Finished{ - timer:TimerFixed::::new(time,timer.time(time)), - }; - Ok(()) - }, - RunState::Finished{..}=>Err(Error::AlreadyFinished), + //code based on std::mem::replace + let dest=&mut self.state; + //let's ignore ownership rules for a moment + unsafe{ + let state=std::ptr::read(dest); + match state{ + RunState::Created=>Err(Error::NotStarted), + RunState::Started{timer}=>{ + 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){