From: Ian Jackson Date: Thu, 28 Apr 2022 00:15:01 +0000 (+0100) Subject: Introduce and use xdata_init X-Git-Tag: otter-1.1.0~434 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8b3f6982f123fb80f8b9e06e3a4590778f57e17e;p=otter.git Introduce and use xdata_init Using get_mut for this seems slightly less careful. Signed-off-by: Ian Jackson --- diff --git a/src/clock.rs b/src/clock.rs index 16fbfbdc..95eabb7f 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -412,7 +412,7 @@ impl PieceSpec for Spec { spec: self.clone(), }; - gpc.xdata_mut(|| State::new(self) )?; + gpc.xdata_init(State::new(self))?; SpecLoaded { p: Box::new(clock), diff --git a/src/dice.rs b/src/dice.rs index 3f47b79a..4b8b388a 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -174,7 +174,7 @@ impl PieceSpec for Spec { let initial_state = { State { cooldown_expires: cooldown_start_value(cooldown_time)? } }; - let _state: &mut State = gpc.xdata_mut(|| initial_state)?; + gpc.xdata_init(initial_state)?; let occ_label = |occ: &OccultSpec| -> String { if occ.label == "" && labels.iter().any(|l| l != "") { diff --git a/src/gamestate.rs b/src/gamestate.rs index 89123d9c..54045328 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -457,6 +457,11 @@ impl GPiece { self.xdata.get_mut_exp()? } + #[throws(IE)] + pub fn xdata_init(&mut self, val: T) -> &mut T { + self.xdata.init(val)? + } + pub fn moveable(&self) -> PieceMoveable { if self.occult.is_active() { PieceMoveable::No } else { self.moveable } @@ -500,6 +505,13 @@ fn xdata_unexpected(got: &dyn PieceXData) -> InternalError { &got, T::dummy(), )) } +fn xdata_unexpectedly_present(got: &dyn PieceXData) -> InternalError { + internal_logic_error(format!( + "\n\ + piece xdata unexpectedly present: {:?}\n", + &got, + )) +} fn xdata_missing() -> InternalError { internal_logic_error(format!( "\n\ @@ -535,6 +547,16 @@ impl PieceXDataState { let xdata = self.as_mut().ok_or_else(|| xdata_missing::())?; xdata_get_mut_inner(xdata)? } + + #[throws(IE)] + fn init(&mut self, val: T) -> &mut T { + if let Some(xdata) = self.as_ref() { + let xdata: &dyn PieceXData = &**xdata; + throw!(xdata_unexpectedly_present(xdata)); + } + let xdata = self.insert(Box::new(val) as _); + xdata_get_mut_inner(xdata)? + } } fn xdata_get_mut_inner<