chiark / gitweb /
Genericise OpOutcomeThunk
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 1 May 2022 10:03:10 +0000 (11:03 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 1 May 2022 10:03:10 +0000 (11:03 +0100)
So that we can centralise resolution.

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

index eda863367ee71b1b0dfcd63b16282f153eb2c5b1..a0ed7c8d14c36ac84e04115358361288e015380a 100644 (file)
@@ -145,7 +145,7 @@ fn api_piece_op<O: op::Complex>(form: Json<ApiPiece<O>>)
   })().and_then(|(thunk, loose_conflict)| Ok((
     match thunk {
       OpOutcomeThunk::Immediate(r) => r,
-      OpOutcomeThunk::Reborrow(f) => f(&mut ig, player, piece)?,
+      OpOutcomeThunk::Reborrow(f) => f(&mut ig, (player, piece))?,
     }, loose_conflict
   ))) {
     Err(APOE::Inapplicable(poe)) => {
@@ -199,7 +199,7 @@ fn api_piece_op<O: op::Complex>(form: Json<ApiPiece<O>>)
     let unprepared = match thunk {
       Err(e) => Err(e),
       Ok(OpHookThunk::Immediate(uu)) => Ok(uu),
-      Ok(OpHookThunk::Reborrow(f)) => f(&mut ig, player),
+      Ok(OpHookThunk::Reborrow(f)) => f(&mut ig, (player,)),
     };
     if let Ok(unprepared) = unprepared.map_err(
       |e| error!("internal error on change hook: {:?}", e));
index b57e21b285471fb3ac8734dca50e46011780f337..44c54dfe40a9883503308d40c0735f2e297cbdb4 100644 (file)
@@ -127,7 +127,7 @@ impl PieceTrait for Banknote {
                   take: MultigrabQty, new_z: ShouldSetZLevel) -> OpOutcomeThunk {
     let currency = self.currency.clone();
     OpOutcomeThunk::Reborrow(Box::new(
-      move |ig: &mut InstanceGuard, player: PlayerId, tpiece: PieceId|
+      move |ig: &mut InstanceGuard, (player, tpiece)|
   {
     ig.fastsplit_split(player, tpiece, show, new_z,
       move |_: &IOccults, _: &GameOccults, gpl: &GPlayer,
index 9e4cdb6397152d419255abb5c3b3dabf3201d4b2..725a25d8162822cff9ea925416cc654b191ac9c9 100644 (file)
@@ -165,38 +165,28 @@ pub enum PieceUpdateOp<NS,ZL> {
   SetZLevelQuiet(ZL),
 }
 
-#[derive(From)]
-pub enum OpOutcomeThunk {
-  Immediate(UpdateFromOpComplex),
-  /// Allows a UI operation full mutable access to the whole Instance.
-  ///
-  /// Use with care!  Eg, you might have to call save_game_and_aux_later.
-  ///
-  /// Adding and removing pieces during play (rather than management)
-  /// is complicated, because we want to avoid having to rewrite the aux.
-  /// file during routine game saves.  `fastsplit.rs` has machinery that
-  /// can achieve this.
-  Reborrow(Box<dyn FnOnce(&mut InstanceGuard, PlayerId, PieceId)
-                   -> Result<UpdateFromOpComplex, ApiPieceOpError>>),
-}
-
 #[derive(From,Educe)]
-#[educe(Default)]
-pub enum OpHookThunk {
+#[educe(Default(bound="T: Default"))]
+pub enum OpOutcomeThunkGeneric<A,T,E> {
   #[educe(Default)]
-  Immediate(UnpreparedUpdates),
-  /// Allows a UI operation full mutable access to the whole Instance.
+  Immediate(T),
+  /// Allows an operation full mutable access to the whole Instance.
   ///
-  /// Use with care!  Eg, you might have to call save_game_and_aux_late.r
+  /// Use with care!  Eg, you might have to call save_game_and_aux_later.
   ///
   /// Adding and removing pieces during play (rather than management)
   /// is complicated, because we want to avoid having to rewrite the aux.
   /// file during routine game saves.  `fastsplit.rs` has machinery that
   /// can achieve this.
-  Reborrow(Box<dyn FnOnce(&mut InstanceGuard, PlayerId)
-                   -> Result<UnpreparedUpdates, InternalError>>),
+  Reborrow(Box<dyn FnOnce(&mut InstanceGuard, A) -> Result<T,E>>),
 }
 
+pub type OpOutcomeThunk = OpOutcomeThunkGeneric<
+    (PlayerId, PieceId), UpdateFromOpComplex, ApiPieceOpError>;
+
+pub type OpHookThunk = OpOutcomeThunkGeneric<
+    (PlayerId,), UnpreparedUpdates, InternalError>;
+
 pub type UpdateFromOpComplex = (
   PieceUpdate,
   UnpreparedUpdates,