chiark / gitweb /
Introduce and use methods for applying UnpreparedUpdates
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 29 Apr 2022 22:54:05 +0000 (23:54 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 30 Apr 2022 10:02:54 +0000 (11:02 +0100)
There are quite a few call sites where the nature of an
UnpreparedUpdates is open-coded.

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

index 8828d570882353c3bd164a24976db94916bf0a84..7c4c2b351b1fe98e301dd2b800c2bcb9ca13900c 100644 (file)
@@ -184,7 +184,7 @@ fn api_piece_op<O: op::Complex>(form: Json<ApiPiece<O>>)
     }
   };
 
-  if let Some(unprepared) = if_chain! {
+  if_chain! {
     let g = &mut *ig;
     if let Some(was_held) = was_held;
     if let Some(gpc) = g.gs.pieces.get_mut(piece);
@@ -196,12 +196,9 @@ fn api_piece_op<O: op::Complex>(form: Json<ApiPiece<O>>)
       piece,
       was_held,
     ).map_err(|e| error!("internal error on change hook: {:?}", e));
-    then { unprepared }
-    else { None }
-  } {
-    let mut prepub = PrepareUpdatesBuffer::new(&mut ig, None);
-    unprepared(&mut prepub);
-    prepub.finish();
+    then {
+      PrepareUpdatesBuffer::only_unprepared(&mut ig, unprepared);
+    }
   }
 
       Ok::<(),Fatal>(())
@@ -216,11 +213,7 @@ fn api_piece_op<O: op::Complex>(form: Json<ApiPiece<O>>)
                              &g.ipieces))
   });
 
-  if let Some(unprepared) = unprepared_outer {
-    let mut prepub = PrepareUpdatesBuffer::new(&mut ig, None);
-    unprepared(&mut prepub);
-    prepub.finish();
-  }
+  PrepareUpdatesBuffer::only_unprepared(&mut ig, unprepared_outer);
 
   ok?;
   ""
index cdbd874eee418f41731b6b77c075dd6f3ba8f823..ba22714f132792e23b618ae1bcec51816641824e 100644 (file)
@@ -819,6 +819,20 @@ impl<'r> PrepareUpdatesBuffer<'r> {
     }
   }
 
+  pub fn add_unprepared(&mut self, unprepared: UnpreparedUpdates) {
+    if let Some(unprepared) = unprepared {
+      unprepared(self);
+    }
+  }
+
+  pub fn only_unprepared(ig: &'r mut Instance, unprepared: UnpreparedUpdates) {
+    if let Some(unprepared) = unprepared {
+      let mut prepub = PrepareUpdatesBuffer::new(ig, None);
+      unprepared(&mut prepub);
+      prepub.finish();
+    }
+  }
+
   pub fn finish(self) { }
 }