chiark / gitweb /
clamped: Refactor return value to not have separate bool
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Feb 2021 12:28:53 +0000 (12:28 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Feb 2021 01:04:41 +0000 (01:04 +0000)
NFC

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

index ac031feb6cb758b464490f0098e1b5015e5efda9..cc5550f67266c806ce4e6de97319ab3b12b5d38b 100644 (file)
@@ -342,15 +342,17 @@ api_route!{
   fn op(&self, a: ApiPieceOpArgs) -> PieceUpdate {
     let ApiPieceOpArgs { gs,piece, .. } = a;
     let pc = gs.pieces.byid_mut(piece).unwrap();
-    let (pos, clamped) = self.0.clamped(gs.table_size);
     let logents = vec![];
-    pc.pos = pos;
-    if clamped {
-      throw!(ApiPieceOpError::PartiallyProcessed(
-        PieceOpError::PosOffTable,
-        logents,
-      ));
-    }
+    match self.0.clamped(gs.table_size) {
+      Ok(pos) => pc.pos = pos,
+      Err(pos) => {
+        pc.pos = pos;
+        throw!(ApiPieceOpError::PartiallyProcessed(
+          PieceOpError::PosOffTable,
+          logents,
+        ));
+      }
+    };
     let update = PieceUpdateOp::Move(self.0);
     (WhatResponseToClientOp::Predictable,
      update, logents).into()
index 2eafdb33cd2afbb37807aa4023b1429d09d33844..daf3ce5549ec438a23755cb176c8590520977d99 100644 (file)
@@ -618,9 +618,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>(
           pos, face,
           xdata: None,
         };
-        if let (_, true) = pc.pos.clamped(gs.table_size) {
-          throw!(SpecError::PosOffTable);
-        }
+        pc.pos.clamped(gs.table_size).map_err(|_| SpecError::PosOffTable)?;
         if pc.zlevel.z > gs.max_z { gs.max_z = pc.zlevel.z.clone() }
         let piece = gs.pieces.as_mut(modperm).insert(pc);
         ig.ipieces.as_mut(modperm).insert(piece, p);
index 6539f38149e09acb49a3cb0fd848e7bbfcbb934f..d1645fcbf69142aacfa1dfaf60f13ff968b3922a 100644 (file)
@@ -212,27 +212,29 @@ impl Timestamp {
 }
 
 pub trait ClampTable: Sized {
-  fn clamped(self, range: Self) -> (Self, bool);
+  fn clamped(self, range: Self) -> Result<Self, Self>;
 }
 
 impl ClampTable for Coord {
-  fn clamped(self, range: Coord) -> (Coord, bool) {
-    if self < 0     { return (0,     true) }
-    if self > range { return (range, true) }
-    return (self, false)
+  fn clamped(self, range: Coord) -> Result<Coord, Coord> {
+    if self < 0     { return Err(0,   ) }
+    if self > range { return Err(range) }
+    return Ok(self)
   }
 }
 
 impl ClampTable for Pos {
-  fn clamped(self, range: Pos) -> (Pos, bool) {
+  fn clamped(self, range: Pos) -> Result<Pos, Pos> {
     let mut output = ArrayVec::new();
-    let mut did = false;
-    for (npos, tdid) in self.0.iter().zip(range.0.iter())
-      .map(|(&pos, &rng)| pos.clamped(rng)) {
-      output.push(npos);
-      did |= tdid;
+    let mut ok = true;
+    for (&pos, &rng) in izip!(self.0.iter(), range.0.iter()) {
+      output.push(match pos.clamped(rng) {
+        Ok(pos) => pos,
+        Err(pos) => { ok = false; pos },
+      })
     }
-    (PosC(output.into_inner().unwrap()), did)
+    let output = PosC(output.into_inner().unwrap());
+    if ok { Ok(output) } else { Err(output) }
   }
 }