Sfoglia il codice sorgente

Use let-else intead of match and if-let.

Jing Yang 3 anni fa
parent
commit
ebdb3a0244
5 ha cambiato i file con 41 aggiunte e 45 eliminazioni
  1. 16 20
      kvraft/src/client.rs
  2. 9 8
      kvraft/src/server.rs
  3. 8 8
      src/election.rs
  4. 6 6
      src/remote_context.rs
  5. 2 3
      tests/agreement_tests.rs

+ 16 - 20
kvraft/src/client.rs

@@ -97,26 +97,22 @@ impl ClerkInner {
                 args,
                 None,
             );
-            if let Some(reply) = reply {
-                match reply.result {
-                    Ok(_) => {
-                        // Discard the used unique_id.
-                        self.unique_id.inc();
-                        break;
-                    }
-                    Err(KVError::Expired) | Err(KVError::Conflict) => {
-                        // The client ID happens to be re-used. The request does
-                        // not fail as "Duplicate", because another client has
-                        // committed more than just the sentinel.
-                        self.unique_id = UniqueIdSequence::new();
-                    }
-                    Err(e) => {
-                        panic!(
-                            "Unexpected error with indefinite retry: {:?}",
-                            e
-                        );
-                    }
-                };
+            let Some(reply) = reply else { continue };
+            match reply.result {
+                Ok(_) => {
+                    // Discard the used unique_id.
+                    self.unique_id.inc();
+                    break;
+                }
+                Err(KVError::Expired) | Err(KVError::Conflict) => {
+                    // The client ID happens to be re-used. The request does
+                    // not fail as "Duplicate", because another client has
+                    // committed more than just the sentinel.
+                    self.unique_id = UniqueIdSequence::new();
+                }
+                Err(e) => {
+                    panic!("Unexpected error with indefinite retry: {:?}", e);
+                }
             };
         }
     }

+ 9 - 8
kvraft/src/server.rs

@@ -276,9 +276,8 @@ impl KVServer {
             return Err(KVError::NotLeader);
         }
 
-        let result_fut = match self.rf.verify_authority_async() {
-            Some(result_fut) => result_fut,
-            None => return Err(KVError::NotLeader),
+        let Some(result_fut) = self.rf.verify_authority_async() else {
+            return Err(KVError::NotLeader);
         };
         let index =
             match tokio::time::timeout(Self::DEFAULT_TIMEOUT, result_fut).await
@@ -406,9 +405,12 @@ impl KVServer {
 
         let result = result_holder.result.clone();
         // Wait for the op to be committed.
-        let result = tokio::time::timeout(timeout, result).await;
+        let Ok(result) = tokio::time::timeout(timeout, result).await else {
+            return Err(CommitError::TimedOut);
+        };
+
         match result {
-            Ok(Ok(Ok(result))) => {
+            Ok(Ok(result)) => {
                 // If the result is OK, all other requests should see "Duplicate".
                 if result_holder.peeks.fetch_add(1, Ordering::Relaxed) == 0 {
                     Ok(result)
@@ -416,9 +418,8 @@ impl KVServer {
                     Err(CommitError::Duplicate(result))
                 }
             }
-            Ok(Ok(Err(e))) => Err(e),
-            Ok(Err(_)) => Err(CommitError::NotLeader),
-            Err(_) => Err(CommitError::TimedOut),
+            Ok(Err(e)) => Err(e),
+            Err(_) => Err(CommitError::NotLeader),
         }
     }
 

+ 8 - 8
src/election.rs

@@ -299,10 +299,9 @@ impl<Command: ReplicableCommand> Raft<Command> {
         let prevotes = Self::spawn_request_votes(&candidate, prevote_args);
         let prevote_results =
             Self::quorum_before_cancelled(prevotes, cancel_token).await;
-        let cancel_token = match prevote_results {
-            QuorumOrCancelled::Accepted(cancel_token) => cancel_token,
+        let QuorumOrCancelled::Accepted(cancel_token) = prevote_results else {
             // Did not get quorum on a prevote. Skip the rest.
-            _ => return,
+            return;
         };
 
         // Advance to the next term.
@@ -349,11 +348,12 @@ impl<Command: ReplicableCommand> Raft<Command> {
                 rpc_client.request_vote(args.clone())
             })
             .await;
-        if let Ok(reply) = reply {
-            RemoteContext::<Command>::term_marker().mark(reply.term);
-            return Some(reply.vote_granted && reply.term == term);
-        }
-        None
+        let Ok(reply) = reply else {
+            return None;
+        };
+
+        RemoteContext::<Command>::term_marker().mark(reply.term);
+        return Some(reply.vote_granted && reply.term == term);
     }
 
     fn spawn_request_votes(

+ 6 - 6
src/remote_context.rs

@@ -63,13 +63,13 @@ impl<Command: 'static> RemoteContext<Command> {
 
     fn fetch_context() -> &'static Self {
         let any_ref = Self::REMOTE_CONTEXT.with(|context| *context.borrow());
-        if let Some(any_ref) = any_ref {
-            any_ref
-                .downcast_ref::<Self>()
-                .expect("Context is set to the wrong type.")
-        } else {
+        let Some(any_ref) = any_ref else {
             panic!("Context is not set");
-        }
+        };
+
+        any_ref
+            .downcast_ref::<Self>()
+            .expect("Context is set to the wrong type.")
     }
 }
 

+ 2 - 3
tests/agreement_tests.rs

@@ -258,9 +258,8 @@ fn count() -> config::Result<()> {
         let start_total = cfg.total_rpcs();
 
         const ITERS: usize = 10;
-        let (term, start_index) = match cfg.leader_start(leader, 1) {
-            Some(pair) => pair,
-            None => continue,
+        let Some((term, start_index)) = cfg.leader_start(leader, 1) else {
+            continue
         };
 
         let mut cmds = vec![];