From: Ian Jackson Date: Sat, 30 Apr 2022 17:07:10 +0000 (+0100) Subject: Introduce and use downcast_piece_fastsplit X-Git-Tag: otter-1.1.0~390 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=9b961d0a533d69fd94b4a7d7a8355f4e6a2119a1;p=otter.git Introduce and use downcast_piece_fastsplit Currency wants this. Signed-off-by: Ian Jackson --- diff --git a/src/currency.rs b/src/currency.rs index 1dad79f6..b57e21b2 100644 --- a/src/currency.rs +++ b/src/currency.rs @@ -131,10 +131,10 @@ impl PieceTrait for Banknote { { ig.fastsplit_split(player, tpiece, show, new_z, move |_: &IOccults, _: &GameOccults, gpl: &GPlayer, - tgpc: &mut GPiece, _tipc: &IPiece, tipc_p: &dyn PieceTrait, + tgpc: &mut GPiece, tipc: &IPiece, ngpc: &mut GPiece| { - let self_: &Banknote = tipc_p.downcast_piece()?; + let self_: &Banknote = tipc.p.show(show).downcast_piece_fastsplit()?; let tgpc_value: &mut Value = tgpc.xdata.get_mut_exp()?; let remaining = tgpc_value.qty.checked_sub(take) diff --git a/src/fastsplit.rs b/src/fastsplit.rs index 847d6a16..e5f1569d 100644 --- a/src/fastsplit.rs +++ b/src/fastsplit.rs @@ -83,11 +83,11 @@ impl InstanceGuard<'_> { #[throws(ApiPieceOpError)] pub fn fastsplit_split( &mut self, player: PlayerId, - tpiece: PieceId, show: ShowUnocculted, tpc_new_z: ShouldSetZLevel, + tpiece: PieceId, _: ShowUnocculted, tpc_new_z: ShouldSetZLevel, implementation: I ) -> UpdateFromOpComplex where I: FnOnce(&IOccults, &GameOccults, &GPlayer, - &mut GPiece, &IPiece, &dyn PieceTrait, + &mut GPiece, &IPiece, &mut GPiece) -> Result { @@ -136,14 +136,9 @@ impl InstanceGuard<'_> { fastsplit: tgpc.fastsplit, }; - let tipc_p = tipc.p - .show(show).downcast_piece::()? - .ipc.as_ref().ok_or_else(|| internal_error_bydebug(tipc))? - .p.show(show); - let (t_pu, t_unprepared) = implementation( &ig.ioccults, &ig.gs.occults, gpl, - tgpc, tipc, tipc_p, + tgpc, tipc, &mut ngpc )?; @@ -226,3 +221,15 @@ impl IFastSplits { }) } } + +#[ext(pub)] +impl<'r> &'r dyn PieceTrait { + #[throws(IE)] + 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)))? + .p.direct_trait_access() // we're just digging down, this is fine + .downcast_piece::

()? + } +} diff --git a/src/prelude.rs b/src/prelude.rs index 6b6402a8..5a1cf43b 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -187,6 +187,9 @@ pub use crate::updates::*; pub use crate::utils::*; pub use crate::ui::*; +pub use crate::gamestate::RefTraitObjectPieceTraitExt as _; +pub use crate::fastsplit::RefTraitObjectPieceTraitExt as _; + pub type SecondarySlotMap = slotmap::secondary::SecondaryMap; pub type SvgData = Vec; pub type Colour = Html;