From e9e6a9aaf87ae42e591ddd03ba7aa55b4a45b026 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 15 Apr 2022 12:54:37 +0100 Subject: [PATCH] dice: Tidy remprop handling This macro was overkill. It existed mostly to avoid uselessly comparing the remaining duration with 0 and storing None when it was there already. That's a daft microoptimisation. Doing it this way instead lets us only do the remprop calculation when it's wanted, and it will let us provide a way to set cooldown_expires to None if and when we have &mut State and can see it's timed out. Signed-off-by: Ian Jackson --- src/dice.rs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/dice.rs b/src/dice.rs index 02b33785..7e583ffc 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -204,20 +204,13 @@ impl PieceSpec for Spec { } } -macro_rules! def_cooldown_remprop { { $name:ident $($mut:tt)? } => { - #[allow(dead_code)] +impl Die { #[throws(IE)] - pub fn $name(&self, state: &$($mut)? State) -> f64 { - let expires = &$($mut)? state.cooldown_expires; - if_let!{ Some(FutureInstant(then)) = *expires; else return Ok(0.) }; + pub fn cooldown_remaining(&self, state: &State) -> Duration { + let expires = &state.cooldown_expires; + if_let!{ Some(FutureInstant(then)) = *expires; else return Ok(default()) }; let now = Instant::now(); - if now > then { - {$( - #[allow(unused_mut)] let $mut _x = (); // repetition count - *expires = None; - )?} - return 0. - } + if now > then { return default() } let remaining = then - now; if remaining > self.cooldown_time { throw!(internal_logic_error(format!( @@ -226,13 +219,15 @@ macro_rules! def_cooldown_remprop { { $name:ident $($mut:tt)? } => { remaining, self.cooldown_time ))) } - remaining.as_secs_f64() / self.cooldown_time.as_secs_f64() + remaining } -} } -impl Die { - def_cooldown_remprop!{ cooldown_remprop } - def_cooldown_remprop!{ cooldown_remprop_mut mut } + #[throws(IE)] + pub fn cooldown_remprop(&self, state: &State) -> f64 { + self.cooldown_remaining(state)?.as_secs_f64() + / + self.cooldown_time .as_secs_f64() + } } #[dyn_upcast] @@ -287,7 +282,7 @@ impl InertPieceTrait for Die { #[throws(IE)] fn svg(&self, f: &mut Html, vpid: VisiblePieceId, face: FaceId, xdata: &PieceXDataState /* use with care! */) { - let state = xdata.get_exp::()?; + let state: &State = xdata.get_exp()?; // This is called by PieceTrait::svg_piece, so face may be non-0 // despite the promise about face in InertPieceTrait. -- 2.30.2