Browse Source

rustfmt and clippy fixes.

Kestrel 2 years ago
parent
commit
4478ed77d1

+ 3 - 6
fleck/examples/unix_socket.rs

@@ -7,7 +7,7 @@ struct SocketArgs {
     #[arg(short, long)]
     server: bool,
 
-    filename: String
+    filename: String,
 }
 
 fn main() {
@@ -23,12 +23,10 @@ fn main() {
     });
 
     let sockref = fleck.with_service(|io: &fleck_core::io::IOService| {
-        let builder = fleck_core::io::UnixSocketBuilder::default()
-            .set_path(args.filename);
+        let builder = fleck_core::io::UnixSocketBuilder::default().set_path(args.filename);
         if args.server {
             builder.serve_mode().build(io)
-        }
-        else {
+        } else {
             builder.connect_mode().build(io)
         }
     });
@@ -49,4 +47,3 @@ fn main() {
     // run and perform all automatic services, but don't do anything interesting...
     fleck.run();
 }
-

+ 4 - 5
fleck/src/fleck_core/crypto.rs

@@ -171,16 +171,15 @@ impl Service for EncryptionService {
 
 impl EncryptionService {
     fn new_node(&self, node: &mut std::rc::Rc<fleck_core::node::Node>) {
-        match node.peer().as_ref() {
-            Some(peer) => self.begin_handshake(peer),
-            None => (),
+        if let Some(peer) = node.peer().as_ref() {
+            self.begin_handshake(peer)
         }
     }
 
     fn begin_handshake(&self, with: &fleck_core::peer::Peer) {
         use rand::SeedableRng;
-        let mut csprng = rand::rngs::StdRng::from_entropy();
-        let secret = x25519_dalek::EphemeralSecret::new(&mut csprng);
+        let csprng = rand::rngs::StdRng::from_entropy();
+        let secret = x25519_dalek::EphemeralSecret::new(csprng);
 
         // send notification with new secret
         let send = || {

+ 2 - 1
fleck/src/fleck_core/discovery.rs

@@ -139,7 +139,8 @@ impl PeerDiscovery {
                     .api
                     .with_service(|ns: &fleck_core::NodeService| *ns.self_node().pubkey()),
                 ttl: 2,
-            }).with_peer(peer)
+            })
+            .with_peer(peer),
         );
     }
 }

+ 11 - 3
fleck/src/fleck_core/io.rs

@@ -66,13 +66,17 @@ impl Service for IOService {
         self.api.create_channel::<super::ReceivePacketChannel>();
         self.api.create_channel::<super::SendPacketChannel>();
         self.api.create_channel::<NewHandlerChannel>();
-        self.api.channel::<NewHandlerChannel>().sub_eat(self, &Self::register_handler);
+        self.api
+            .channel::<NewHandlerChannel>()
+            .sub_eat(self, Self::register_handler);
     }
 }
 
 impl IOService {
     pub fn register_handler(&self, handler: Rc<dyn FdHandler>) {
-        self.handlers.borrow_mut().insert(handler.interest().token, handler);
+        self.handlers
+            .borrow_mut()
+            .insert(handler.interest().token, handler);
     }
 
     pub fn register_interest(&self, fd: RawFd) -> InterestRegistration {
@@ -98,7 +102,11 @@ impl IOService {
     }
 
     pub fn unregister_interest(&self, reg: InterestRegistration) {
-        reg.poll.borrow().registry().deregister(&mut mio::unix::SourceFd(&reg.fd)).expect("Couldn't deregister file descriptor?");
+        reg.poll
+            .borrow()
+            .registry()
+            .deregister(&mut mio::unix::SourceFd(&reg.fd))
+            .expect("Couldn't deregister file descriptor?");
     }
 
     pub(crate) fn run(&self) {

+ 1 - 1
fleck/src/fleck_core/io/timerfd.rs

@@ -39,7 +39,7 @@ impl TimerFdBuilder {
             trigger: self.trigger.expect("building timerfd with no trigger?"),
         });
 
-        io.api.queue::<super::NewHandlerChannel>(sock.clone());
+        io.api.queue::<super::NewHandlerChannel>(sock);
     }
 }
 

+ 32 - 27
fleck/src/fleck_core/io/unix.rs

@@ -1,9 +1,15 @@
-use std::{os::fd::AsRawFd, rc::Rc, cell::RefCell, collections::{VecDeque, HashMap}, io::{Read, Write}};
 use super::fleck_core;
+use std::{
+    cell::RefCell,
+    collections::VecDeque,
+    io::{Read, Write},
+    os::fd::AsRawFd,
+    rc::Rc,
+};
 
-use mio::net::{UnixDatagram, UnixListener, UnixStream};
+use mio::net::{UnixListener, UnixStream};
 
-use super::{IOService, InterestRegistration, FdHandler};
+use super::{FdHandler, IOService, InterestRegistration};
 
 #[derive(Default)]
 pub struct UnixSocketBuilder {
@@ -33,9 +39,7 @@ impl UnixSocketBuilder {
             false => UnixSocket::Server(UnixServerSocket::build(io, self.path.unwrap())),
         };
 
-        UnixSocketRef {
-            sock
-        }
+        UnixSocketRef { sock }
     }
 }
 
@@ -46,9 +50,7 @@ pub struct UnixSocketRef {
 impl UnixSocketRef {
     pub fn connected_peer(&self) -> fleck_core::Peer {
         match &self.sock {
-            UnixSocket::Client(client) => {
-                client.peer.get().unwrap().clone()
-            },
+            UnixSocket::Client(client) => client.peer.get().unwrap().clone(),
             UnixSocket::Server(_) => {
                 panic!("Can't get the connected peer for a UNIX server socket!")
             }
@@ -89,15 +91,19 @@ impl UnixClientSocket {
         });
 
         let peer = fleck_core::Peer {
-            data: Rc::new(fleck_core::peer::PeerData::new(sock.clone(), fleck_core::peer::PeerAddress::Stream)),
+            data: Rc::new(fleck_core::peer::PeerData::new(
+                sock.clone(),
+                fleck_core::peer::PeerAddress::Stream,
+            )),
         };
 
-        sock.peer.set(peer).expect("somehow couldn't initialize the once_cell?");
+        sock.peer
+            .set(peer)
+            .expect("somehow couldn't initialize the once_cell?");
 
         io.api.queue::<super::NewHandlerChannel>(sock.clone());
 
         sock
-        
     }
 }
 
@@ -119,9 +125,11 @@ impl FdHandler for UnixClientSocket {
             let data = rq.make_contiguous();
             let packet_len = u16::from_be_bytes(data[0..2].try_into().unwrap()) as usize;
             if data.len() < (packet_len + 2) {
-                break
+                break;
             }
-            if let Ok(mut msg) = bincode::deserialize::<fleck_core::msg::Message>(&data[2..(packet_len + 2)]) {
+            if let Ok(mut msg) =
+                bincode::deserialize::<fleck_core::msg::Message>(&data[2..(packet_len + 2)])
+            {
                 msg.peer = self.peer.get().cloned();
                 self.api.queue::<fleck_core::ReceivePacketChannel>(msg);
             }
@@ -134,11 +142,10 @@ impl FdHandler for UnixClientSocket {
 
         while sq.len() > 0 {
             let data = sq.make_contiguous();
-            if let Ok(bytes) = self.socket.borrow_mut().write(&data) {
+            if let Ok(bytes) = self.socket.borrow_mut().write(data) {
                 sq.drain(0..bytes);
-            }
-            else {
-                return
+            } else {
+                return;
             }
         }
 
@@ -163,18 +170,19 @@ pub struct UnixServerSocket {
 
 impl UnixServerSocket {
     fn build(io: &IOService, path: String) -> Rc<Self> {
-
         match std::fs::remove_file(&path) {
             Ok(_) => (),
             Err(e) => match e.kind() {
                 std::io::ErrorKind::PermissionDenied => {
                     panic!("couldn't remove existing socket file!")
-                },
+                }
                 std::io::ErrorKind::NotFound => {
                     // this is perfectly okay!
-                },
-                k => { panic!("Unexpected error removing socket file: {:?}", k) }
-            }
+                }
+                k => {
+                    panic!("Unexpected error removing socket file: {:?}", k)
+                }
+            },
         }
 
         let socket = UnixListener::bind(path).expect("couldn't bind to given path");
@@ -190,8 +198,6 @@ impl UnixServerSocket {
 
         sock
     }
-
-
 }
 
 impl FdHandler for UnixServerSocket {
@@ -205,8 +211,7 @@ impl FdHandler for UnixServerSocket {
             if let Err(err) = client {
                 if err.kind() == std::io::ErrorKind::WouldBlock {
                     break;
-                }
-                else {
+                } else {
                     panic!("Unexpected error occurred while accepting new UNIX socket client");
                 }
             }

+ 4 - 0
fleck/src/service/event.rs

@@ -1,3 +1,7 @@
+// we have some complex types in here, but it's okay. imo splitting things out makes them *harder*
+// to follow in this case.
+#![allow(clippy::type_complexity)]
+
 use std::cell::RefCell;
 use std::{
     collections::{HashMap, VecDeque},