From: Ian Jackson Date: Wed, 16 Sep 2020 22:51:08 +0000 (+0100) Subject: make resolve_spec_face a method on Piece not PieceSpec X-Git-Tag: otter-0.2.0~934 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=df0206b99ba216838b926558fd464283202a9e73;p=otter.git make resolve_spec_face a method on Piece not PieceSpec This will avoid recomputation for piece specs which are just a name. NFC. Signed-off-by: Ian Jackson --- diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index 407d427e..4dc4daec 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -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, diff --git a/src/gamestate.rs b/src/gamestate.rs index 5bc4c0ca..070ce2c1 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -83,6 +83,9 @@ type SE = SVGProcessingError; #[typetag::serde] pub trait Piece : Send + Debug { + fn resolve_spec_face(&self, face : Option) + -> Result; + // #[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,SpecError>; - fn resolve_spec_face(&self, face : Option) - -> Result; } // ========== implementations ========== diff --git a/src/pieces.rs b/src/pieces.rs index df4b1d5b..6eee0e00 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -115,6 +115,12 @@ impl Piece for SimpleShape { format!("a {}", self.desc.0) }) } + #[throws(SpecError)] + fn resolve_spec_face(&self, face: Option) -> 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, - face: Option) - -> 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 { - 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 { - simple_resolve_spec_face(&self.faces, face)? - } }