chiark / gitweb /
dice: Tidy remprop handling
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 11:54:37 +0000 (12:54 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 15 Apr 2022 21:43:43 +0000 (22:43 +0100)
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 <ijackson@chiark.greenend.org.uk>
src/dice.rs

index 02b33785ebe89c7654b1f1b35f18732a634a650f..7e583ffc480bcb25248e1c5431477b5393975d73 100644 (file)
@@ -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::<State>()?;
+    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.