chiark / gitweb /
Introduce PieceBaseTrait and put nfaces() in it
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 14 Apr 2022 19:03:34 +0000 (20:03 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 14 Apr 2022 19:03:34 +0000 (20:03 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/clock.rs
src/deck.rs
src/gamestate.rs
src/hand.rs
src/pieces.rs
src/shapelib.rs

index 69bcf432900897d8b99690fd27bcbdd407d9f0ae..5547309f4298ce955a399ab27f61f10b9c768b4a 100644 (file)
@@ -432,10 +432,12 @@ impl OutlineTrait for Clock {
   }
 }
 
-#[typetag::serde(name="ChessClock")]
-impl PieceTrait for Clock {
+impl PieceBaseTrait for Clock {
   fn nfaces(&self) -> RawFaceId { 1 }
+}
 
+#[typetag::serde(name="ChessClock")]
+impl PieceTrait for Clock {
   #[throws(IE)]
   fn svg_piece(&self, f: &mut Html, gpc: &GPiece, gs: &GameState,
                vpid: VisiblePieceId) {
index e5eae521fbb94f5b6d7cf6fabbe21892bb1d4814..f03f1c6382b77f6079e083f1900ded6554b602d2 100644 (file)
@@ -96,9 +96,12 @@ impl Deck {
   }
 }
 
+impl PieceBaseTrait for Deck {
+  fn nfaces(&self) -> RawFaceId { 1 }
+}
+
 #[typetag::serde]
 impl PieceTrait for Deck {
-  fn nfaces(&self) -> RawFaceId { 1 }
   #[throws(IE)]
   fn svg_piece(&self, f: &mut Html, gpc: &GPiece,
                gs: &GameState, _vpid: VisiblePieceId) {
index ec7361df390fd7122bb133b653a39131b1cba7d3..4d060587663d9542dafb3f1dab090b3ed84dc28b 100644 (file)
@@ -173,11 +173,13 @@ pub struct UoDescription {
   pub wrc: WhatResponseToClientOp,
 }
 
-#[typetag::serde] // usual variable: p
-pub trait PieceTrait: OutlineTrait + Send + Debug + 'static {
-  /// by convention, occult face is nfaces-1
+pub trait PieceBaseTrait: OutlineTrait + Send + Debug + 'static {
+  /// By convention, occult face is nfaces-1
   fn nfaces(&self) -> RawFaceId;
+}
 
+#[typetag::serde] // usual variable: p
+pub trait PieceTrait: PieceBaseTrait + Send + Debug + 'static {
   #[throws(InternalError)]
   fn add_ui_operations(&self, _: ShowUnocculted,
                        _upd: &mut Vec<UoDescription>,
@@ -236,9 +238,7 @@ pub trait PieceTrait: OutlineTrait + Send + Debug + 'static {
 }
 
 #[typetag::serde]
-pub trait InertPieceTrait: OutlineTrait {
-  fn nfaces(&self) -> RawFaceId;
-
+pub trait InertPieceTrait: PieceBaseTrait {
   /// When used for occultated version of another object,
   /// face used is always default, regardless of nfaces.
   fn svg(&self, f: &mut Html, id: VisiblePieceId, face: FaceId)
index a07254139c37457d0a1090c051544f939ee0d632..78256507939a740c2872920f29fdc3aac219823e 100644 (file)
@@ -146,9 +146,12 @@ impl Behaviour {
   }
 }
 
+impl PieceBaseTrait for Hand {
+  fn nfaces(&self) -> RawFaceId { 1 }
+}
+
 #[typetag::serde]
 impl PieceTrait for Hand {
-  fn nfaces(&self) -> RawFaceId { 1 }
   #[throws(IE)]
   fn svg_piece(&self, f: &mut Html, gpc: &GPiece,
                gs: &GameState, _vpid: VisiblePieceId) {
index f1130d8a5ef943dd7f23d7ed41cf484c4eb86959..e96072bfc1f86e5ff0cc7ad7164754e75ac3dd01 100644 (file)
@@ -126,6 +126,10 @@ impl<Desc, Outl:'static> OutlineTrait for GenericSimpleShape<Desc, Outl>
 }
 //    let edge_attrs = format!(r##"stroke-width="" stroke"##
 
+impl PieceBaseTrait for SimpleShape {
+  fn nfaces(&self) -> RawFaceId { self.count_faces() }
+}
+
 #[typetag::serde]
 impl PieceTrait for SimpleShape {
   #[throws(IE)]
@@ -143,17 +147,12 @@ impl PieceTrait for SimpleShape {
     };
     r
   }
-  fn nfaces(&self) -> RawFaceId {
-    self.count_faces()
-  }
 
   fn itemname(&self) -> &str { self.itemname() }
 }
 
 #[typetag::serde]
 impl InertPieceTrait for SimpleShape {
-  fn nfaces(&self) -> RawFaceId { PieceTrait::nfaces(self) }
-
   #[throws(IE)]
   fn svg(&self, f: &mut Html, _vpid: VisiblePieceId, face: FaceId) {
     self.svg_piece_raw(f, face, &mut |_|Ok(()))?; 
index a3b83dd2d153eb23bd8d586bbfb5971b2d3ef7c1..4f06ef7405963d175fbd0b4425f4e1504c57081e 100644 (file)
@@ -223,10 +223,11 @@ impl OutlineTrait for ItemInertForOcculted { delegate! { to self.outline {
   fn thresh_dragraise(&self) -> Result<Option<Coord>, IE>;
   fn bbox_approx(&self) -> Result<Rect, IE>;
 }}}
+impl PieceBaseTrait for ItemInertForOcculted {
+  fn nfaces(&self) -> RawFaceId { 1 }
+}
 #[typetag::serde(name="Lib")]
 impl InertPieceTrait for ItemInertForOcculted {
-  fn nfaces(&self) -> RawFaceId { 1 }
-
   #[throws(IE)]
   fn svg(&self, f: &mut Html, _: VisiblePieceId, face: FaceId) {
     if face != FaceId::default() {
@@ -364,14 +365,16 @@ impl Item {
   }
 }
 
-#[typetag::serde(name="Lib")]
-impl PieceTrait for Item {
+impl PieceBaseTrait for Item {
   fn nfaces(&self) -> RawFaceId {
     (self.faces.len()
      + self.back.iter().count())
       .try_into().unwrap()
   }
+}
 
+#[typetag::serde(name="Lib")]
+impl PieceTrait for Item {
   #[throws(IE)]
   fn svg_piece(&self, f: &mut Html, gpc: &GPiece,
                _gs: &GameState, vpid: VisiblePieceId) {
@@ -388,8 +391,6 @@ impl PieceTrait for Item {
 
 #[typetag::serde(name="LibItem")]
 impl InertPieceTrait for Item {
-  fn nfaces(&self) -> RawFaceId { <Item as PieceTrait>::nfaces(self) }
-
   #[throws(IE)]
   fn svg(&self, f: &mut Html, id: VisiblePieceId, face: FaceId) {
     self.svg_face(f, face, id)?;