From: Ian Jackson Date: Sat, 30 Apr 2022 17:47:21 +0000 (+0100) Subject: Introduce and use PieceTraitDowncastFailed error X-Git-Tag: otter-1.1.0~389 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=0199e0189a84e2744158967766088a4e4521350c;p=otter.git Introduce and use PieceTraitDowncastFailed error We want this to be cheap to make - not InternalError which contains a stack trace - so that we can use downcast_piece in contexts where it's not an error. Signed-off-by: Ian Jackson --- diff --git a/src/fastsplit.rs b/src/fastsplit.rs index e5f1569d..b8930072 100644 --- a/src/fastsplit.rs +++ b/src/fastsplit.rs @@ -224,11 +224,11 @@ impl IFastSplits { #[ext(pub)] impl<'r> &'r dyn PieceTrait { - #[throws(IE)] + #[throws(PieceTraitDowncastFailed<'r>)] fn downcast_piece_fastsplit(self) -> &'r P { self.downcast_piece::()? - .ipc.as_ref().ok_or_else(|| internal_logic_error( - format!("downcast_piece_fastsplit not fastsplit {:?}", self)))? + .ipc.as_ref().ok_or_else( + || PieceTraitDowncastFailed { p: self, why: "unresolved fastsplit"})? .p.direct_trait_access() // we're just digging down, this is fine .downcast_piece::

()? } diff --git a/src/gamestate.rs b/src/gamestate.rs index 57af8d30..512db5ff 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -421,15 +421,32 @@ impl Timestamp { } } +#[derive(Error, Debug)] +#[error("{self:?}")] +pub struct PieceTraitDowncastFailed<'p> { + pub p: &'p dyn PieceTrait, + pub why: &'static str, +} + #[ext(pub)] impl<'r> &'r dyn PieceTrait { - #[throws(IE)] + #[throws(PieceTraitDowncastFailed<'r>)] fn downcast_piece(self) -> &'r P { - self.downcast_ref::

().ok_or_else(|| internal_logic_error(format!( - "downcaste_piece failure! got: {:?}", &self)))? + self.downcast_ref::

().ok_or_else( + || PieceTraitDowncastFailed { p: self, why: "piece" })? } } +impl<'p> From> for InternalError { + fn from(e: PieceTraitDowncastFailed<'p>) -> InternalError { + internal_logic_error(format!( + "downcaste_piece failure {}! got: {:?}", &e.why, &e.p)) + } +} +impl<'p> From> for ApiPieceOpError { + fn from(e: PieceTraitDowncastFailed<'p>) -> ApiPieceOpError { e.into() } +} + // ---------- positions and ClampTable ---------- #[derive(Error,Debug,Copy,Clone)]