chiark / gitweb /
Provide PieceState xdata
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 13 Feb 2021 21:40:32 +0000 (21:40 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 13 Feb 2021 21:43:32 +0000 (21:43 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/cmdlistener.rs
src/gamestate.rs
src/imports.rs

index b9e4cc338999da3fd43be676ab20b0833c9a762c..67aa47c6cef0c8c3ba75290d8fe68bd84f6433ba 100644 (file)
@@ -605,6 +605,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>(
           angle,
           gen: gs.gen,
           pos, face,
+          xdata: None,
         };
         if let (_, true) = pc.pos.clamped(gs.table_size) {
           throw!(SpecError::PosOffTable);
index dfb29fdd4cec529c3455c4beabe03844eddf10a3..a50315e10b1a2036c4d1fab7a685b99c0efa8680 100644 (file)
@@ -82,6 +82,7 @@ pub struct PieceState {
   pub gen: Generation,
   pub lastclient: ClientId,
   pub gen_before_lastclient: Generation,
+  pub xdata: Option<Box<dyn PieceXData>>,
 }
 
 #[derive(Debug,Serialize,Deserialize)]
@@ -97,6 +98,13 @@ pub struct CommittedLogEntry {
 
 // ---------- piece trait, and rendering ----------
 
+#[typetag::serde(tag="type")]
+pub trait PieceXData: Any + Debug + Send + 'static {
+  fn default() -> Box<dyn PieceXData> where Self: Default {
+    Box::new(<Self as Default>::default())
+  }
+}
+
 #[typetag::serde]
 pub trait Outline: Send + Debug {
   fn surround_path(&self, pri: &PieceRenderInstructions) -> Result<Html, IE>;
@@ -262,6 +270,14 @@ impl PieceState {
       uos        : p.ui_operations()?,
     }
   }
+
+  #[throws(IE)]
+  pub fn xdata<T:PieceXData+Default>(&mut self) -> &mut T {
+    let m = format!("piece xdata unexpectedly {:?}", &self.xdata);
+    let xdata = self.xdata
+      .get_or_insert_with(|| <T as PieceXData>::default());
+    Any::downcast_mut(xdata).ok_or_else(|| internal_logic_error(m))?
+  }
 }
 
 pub trait PieceExt {
index aa1daaa64d15e60ca9fa22ce70431db7392f5ba3..2146afa3875e75cd7b92312aaca3a55fb27f4c03 100644 (file)
@@ -2,6 +2,7 @@
 // SPDX-License-Identifier: AGPL-3.0-or-later
 // There is NO WARRANTY.
 
+pub use std::any::Any;
 pub use std::borrow::Borrow;
 pub use std::borrow::Cow;
 pub use std::cmp::{self, max, min, Ordering};