unsafe 😱
This commit is contained in:
parent
3ef8ddb66a
commit
91608d5100
25
src/run.rs
25
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::<Realtime,Paused>::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){
|
||||
|
Loading…
Reference in New Issue
Block a user