Selaa lähdekoodia

communicator: fix clippy warnings

Lennart Braun 1 vuosi sitten
vanhempi
commit
5fb79bcbcb
3 muutettua tiedostoa jossa 16 lisäystä ja 18 poistoa
  1. 6 9
      communicator/src/communicator.rs
  2. 0 1
      communicator/src/lib.rs
  3. 10 8
      communicator/src/tcp.rs

+ 6 - 9
communicator/src/communicator.rs

@@ -90,7 +90,7 @@ impl ReceiverThread {
     }
 
     pub fn get_stats(&self) -> [usize; 2] {
-        self.stats.lock().unwrap().clone()
+        *self.stats.lock().unwrap()
     }
 
     pub fn reset_stats(&mut self) {
@@ -230,8 +230,7 @@ impl AbstractCommunicator for Communicator {
                 Ok(())
             }
             None => Err(Error::LogicError(format!(
-                "SenderThread for party {} not found",
-                party_id
+                "SenderThread for party {party_id} not found"
             ))),
         }
     }
@@ -246,8 +245,7 @@ impl AbstractCommunicator for Communicator {
                 Ok(())
             }
             None => Err(Error::LogicError(format!(
-                "SenderThread for party {} not found",
-                party_id
+                "SenderThread for party {party_id} not found"
             ))),
         }
     }
@@ -256,8 +254,7 @@ impl AbstractCommunicator for Communicator {
         match self.receiver_threads.get_mut(&party_id) {
             Some(t) => t.receive::<T>(),
             None => Err(Error::LogicError(format!(
-                "ReceiverThread for party {} not found",
-                party_id
+                "ReceiverThread for party {party_id} not found"
             ))),
         }
     }
@@ -265,11 +262,11 @@ impl AbstractCommunicator for Communicator {
     fn shutdown(&mut self) {
         self.sender_threads.drain().for_each(|(party_id, t)| {
             t.join()
-                .expect(&format!("join of sender thread {party_id} failed"))
+                .unwrap_or_else(|_| panic!("join of sender thread {party_id} failed"))
         });
         self.receiver_threads.drain().for_each(|(party_id, t)| {
             t.join()
-                .expect(&format!("join of receiver thread {party_id} failed"))
+                .unwrap_or_else(|_| panic!("join of receiver thread {party_id} failed"))
         });
     }
 

+ 0 - 1
communicator/src/lib.rs

@@ -3,7 +3,6 @@ pub mod tcp;
 pub mod unix;
 
 use bincode::error::{DecodeError, EncodeError};
-use serde;
 use std::collections::HashMap;
 use std::io::Error as IoError;
 use std::sync::mpsc::{RecvError, SendError};

+ 10 - 8
communicator/src/tcp.rs

@@ -38,14 +38,13 @@ fn tcp_connect(
     fn connect_socket(host: &str, port: u16, timeout_seconds: usize) -> Result<TcpStream, Error> {
         // try every 100ms
         for _ in 0..(10 * timeout_seconds) {
-            match TcpStream::connect((host, port)) {
-                Ok(socket) => return Ok(socket),
-                Err(_) => (),
+            if let Ok(socket) = TcpStream::connect((host, port)) {
+                return Ok(socket);
             }
             thread::sleep(Duration::from_millis(100));
         }
         match TcpStream::connect((host, port)) {
-            Ok(socket) => return Ok(socket),
+            Ok(socket) => Ok(socket),
             Err(e) => Err(Error::IoError(e)),
         }
     }
@@ -53,7 +52,10 @@ fn tcp_connect(
     let mut stream = connect_socket(host, port, timeout_seconds)?;
     {
         // send our party id
-        stream.write(&(my_id as u32).to_be_bytes())?;
+        let bytes_written = stream.write(&(my_id as u32).to_be_bytes())?;
+        if bytes_written != 4 {
+            return Err(Error::ConnectionSetupError);
+        }
         // check that we talk to the right party
         let mut other_id_bytes = [0u8; 4];
         stream.read_exact(&mut other_id_bytes)?;
@@ -84,7 +86,7 @@ fn tcp_accept_connections(
         })
         .collect();
     // if nobody should connect to us, we are done
-    if expected_parties.len() == 0 {
+    if expected_parties.is_empty() {
         return Ok(output);
     }
     // create a listender and iterate over incoming connections
@@ -108,11 +110,11 @@ fn tcp_accept_connections(
         expected_parties.remove(&other_id);
         output.insert(other_id, stream);
         // check if we have received connections from every party
-        if expected_parties.len() == 0 {
+        if expected_parties.is_empty() {
             break;
         }
     }
-    if expected_parties.len() > 0 {
+    if !expected_parties.is_empty() {
         Err(Error::ConnectionSetupError)
     } else {
         Ok(output)