This will avoid recomputation for piece specs which are just a name.
NFC.
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
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,
#[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;
#[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 ==========
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 {
}
}
-#[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)]
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]
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)?
- }
}