chiark / gitweb /
make resolve_spec_face a method on Piece not PieceSpec
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 16 Sep 2020 22:51:08 +0000 (23:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 16 Sep 2020 22:51:08 +0000 (23:51 +0100)
This will avoid recomputation for piece specs which are just a name.

NFC.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/cmdlistener.rs
src/gamestate.rs
src/pieces.rs

index 407d427ed006c0509cf22f10d7757411d111af1b..4dc4daec1a4a5219905395205a034d3a7eadfc54 100644 (file)
@@ -239,12 +239,12 @@ fn execute_game_insn(cs: &CommandStream,
       let count = count.unwrap_or(1);
       if count > CREATE_PIECES_MAX { throw!(LimitExceeded) }
       let posd = posd.unwrap_or(DEFAULT_POS_DELTA);
-      let face = info.resolve_spec_face(face)?;
 
       let mut updates = Vec::with_capacity(count as usize);
       let mut pos = pos.unwrap_or(DEFAULT_POS_START);
       for i in 0..count {
         let p = info.load()?;
+        let face = p.resolve_spec_face(face)?;
         let z = ZCoord(gs.max_z.0 + (i + 1) as f64);
         let pc = PieceState {
           held: None,
index 5bc4c0ca87d42daed36821dc15d076bc94526c40..070ce2c177edfceeb86d6e53dd3dcccce67bf8b6 100644 (file)
@@ -83,6 +83,9 @@ type SE = SVGProcessingError;
 
 #[typetag::serde]
 pub trait Piece : Send + Debug {
+  fn resolve_spec_face(&self, face : Option<FaceId>)
+                       -> Result<FaceId,SpecError>;
+
   // #[throws] doesn't work here for some reason
   fn svg_piece(&self, f: &mut Html, pri: &PieceRenderInstructions) -> IR;
 
@@ -112,8 +115,6 @@ pub struct PieceRenderInstructions {
 #[typetag::serde(tag="type")]
 pub trait PieceSpec : Debug {
   fn load(&self) -> Result<Box<dyn Piece>,SpecError>;
-  fn resolve_spec_face(&self, face : Option<FaceId>)
-                       -> Result<FaceId,SpecError>;
 }
 
 // ========== implementations ==========
index df4b1d5bb11e17711c3db1fa5c94c4599db07c68..6eee0e0090dc3c81a25566263c4baa9fcb56901f 100644 (file)
@@ -115,6 +115,12 @@ impl Piece for SimpleShape {
       format!("a {}", self.desc.0)
     })
   }
+  #[throws(SpecError)]
+  fn resolve_spec_face(&self, face: Option<FaceId>) -> FaceId {
+    let face = face.unwrap_or_default();
+    self.colours.get(face).ok_or(SpecError::FaceNotFound)?;
+    face
+  }
 }
 
 impl SimpleShape {
@@ -132,15 +138,6 @@ impl SimpleShape {
   }
 }
 
-#[throws(SpecError)]
-fn simple_resolve_spec_face(faces: &IndexSlice<FaceId,[ColourSpec]>,
-                            face: Option<FaceId>)
-                            -> FaceId {
-  let face = face.unwrap_or_default();
-  faces.get(face).ok_or(SpecError::FaceNotFound)?;
-  face
-}
-
 #[typetag::serde]
 impl PieceSpec for piece_specs::Disc {
   #[throws(SpecError)]
@@ -154,10 +151,6 @@ impl PieceSpec for piece_specs::Disc {
     SimpleShape::new_from_path(Html::lit("circle"), path, self.diam,
                                &self.faces)?
   }
-  #[throws(SpecError)]
-  fn resolve_spec_face(&self, face: Option<FaceId>) -> FaceId {
-    simple_resolve_spec_face(&self.faces, face)?
-  }
 }
 
 #[typetag::serde]
@@ -174,8 +167,4 @@ impl PieceSpec for piece_specs::Square {
     SimpleShape::new_from_path(Html::lit("square"), path, (x+y+1)/2,
                                &self.faces)?
   }
-  #[throws(SpecError)]
-  fn resolve_spec_face(&self, face: Option<FaceId>) -> FaceId {
-    simple_resolve_spec_face(&self.faces, face)?
-  }
 }