From: Ian Jackson Date: Sat, 13 Feb 2021 21:40:32 +0000 (+0000) Subject: Provide PieceState xdata X-Git-Tag: otter-0.4.0~509 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=386c7d5533cdaabbf88bbd7aff6d65754826dae3;p=otter.git Provide PieceState xdata Signed-off-by: Ian Jackson --- diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index b9e4cc33..67aa47c6 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -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); diff --git a/src/gamestate.rs b/src/gamestate.rs index dfb29fdd..a50315e1 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -82,6 +82,7 @@ pub struct PieceState { pub gen: Generation, pub lastclient: ClientId, pub gen_before_lastclient: Generation, + pub xdata: Option>, } #[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 where Self: Default { + Box::new(::default()) + } +} + #[typetag::serde] pub trait Outline: Send + Debug { fn surround_path(&self, pri: &PieceRenderInstructions) -> Result; @@ -262,6 +270,14 @@ impl PieceState { uos : p.ui_operations()?, } } + + #[throws(IE)] + pub fn xdata(&mut self) -> &mut T { + let m = format!("piece xdata unexpectedly {:?}", &self.xdata); + let xdata = self.xdata + .get_or_insert_with(|| ::default()); + Any::downcast_mut(xdata).ok_or_else(|| internal_logic_error(m))? + } } pub trait PieceExt { diff --git a/src/imports.rs b/src/imports.rs index aa1daaa6..2146afa3 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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};