chiark / gitweb /
Introduce and use downcast_piece_fastsplit
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 30 Apr 2022 17:07:10 +0000 (18:07 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 30 Apr 2022 17:07:10 +0000 (18:07 +0100)
Currency wants this.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/currency.rs
src/fastsplit.rs
src/prelude.rs

index 1dad79f60d410587bc15c887a8ef5f31f3352adc..b57e21b285471fb3ac8734dca50e46011780f337 100644 (file)
@@ -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)
index 847d6a16d6de9f8c117d123521c2a767ae360c40..e5f1569d5de2cda80e7e4ed9887bbf34e29d827c 100644 (file)
@@ -83,11 +83,11 @@ impl InstanceGuard<'_> {
   #[throws(ApiPieceOpError)]
   pub fn fastsplit_split<I>(
     &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<UpdateFromOpComplex, ApiPieceOpError>
   {
@@ -136,14 +136,9 @@ impl InstanceGuard<'_> {
       fastsplit:     tgpc.fastsplit,
     };
 
-    let tipc_p = tipc.p
-      .show(show).downcast_piece::<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<P: PieceTrait>(self) -> &'r P {
+    self.downcast_piece::<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::<P>()?
+  }
+}
index 6b6402a85c12deb58e40c23e679bde378fb9b963..5a1cf43bd71b6213c287f520f6d489f868372db3 100644 (file)
@@ -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<K,V> = slotmap::secondary::SecondaryMap<K,V>;
 pub type SvgData = Vec<u8>;
 pub type Colour = Html;