Browse Source

rustfmt pass.

Kestrel 2 years ago
parent
commit
970215d6e4

+ 6 - 3
fleck/examples/simple_node.rs

@@ -1,6 +1,6 @@
-use std::net::{SocketAddr, IpAddr};
+use std::net::{IpAddr, SocketAddr};
 
-use fleck::{prelude::*, fleck_core};
+use fleck::{fleck_core, prelude::*};
 
 fn main() {
     pretty_env_logger::init_timed();
@@ -9,7 +9,10 @@ fn main() {
 
     fleck.with_service(|io: &fleck_core::io::IOService| {
         fleck_core::io::UdpSocketBuilder::default()
-            .bind_to(SocketAddr::new(std::net::Ipv4Addr::UNSPECIFIED.into(), 3535))
+            .bind_to(SocketAddr::new(
+                std::net::Ipv4Addr::UNSPECIFIED.into(),
+                3535,
+            ))
             .join_multicast("239.0.239.0".parse().unwrap(), "0.0.0.0".parse().unwrap())
             .build(io);
     });

+ 2 - 2
fleck/src/fleck_core.rs

@@ -1,9 +1,9 @@
 // pub(crate) mod lowlevel;
 // mod nodes;
 // pub mod vchannel;
-pub mod peer;
-pub mod msg;
 pub mod io;
+pub mod msg;
+pub mod peer;
 
 // pub use nodes::Node;
 // pub use nodes::Nodes;

+ 56 - 32
fleck/src/fleck_core/io.rs

@@ -1,4 +1,9 @@
-use std::{rc::Rc, cell::{RefCell, Cell}, os::unix::prelude::{RawFd, AsRawFd}, collections::{HashMap, VecDeque}};
+use std::{
+    cell::{Cell, RefCell},
+    collections::{HashMap, VecDeque},
+    os::unix::prelude::{AsRawFd, RawFd},
+    rc::Rc,
+};
 
 use crate::prelude::*;
 
@@ -12,11 +17,11 @@ pub struct InterestRegistration {
 impl InterestRegistration {
     pub fn update_interest(&self, to: mio::Interest) {
         if self.interest.get() != to {
-            self.poll.borrow().registry().reregister(
-                &mut mio::unix::SourceFd(&self.fd),
-                self.token,
-                to
-            ).expect("couldn't update FD interest");
+            self.poll
+                .borrow()
+                .registry()
+                .reregister(&mut mio::unix::SourceFd(&self.fd), self.token, to)
+                .expect("couldn't update FD interest");
             self.interest.set(to);
         }
     }
@@ -37,7 +42,9 @@ pub struct IOService {
 impl Default for IOService {
     fn default() -> Self {
         Self {
-            poll: Rc::new(RefCell::new(mio::Poll::new().expect("couldn't create poll?"))),
+            poll: Rc::new(RefCell::new(
+                mio::Poll::new().expect("couldn't create poll?"),
+            )),
             next_token: Cell::new(mio::Token(1)),
             handlers: Default::default(),
         }
@@ -60,11 +67,15 @@ impl IOService {
         let token = self.next_token.get();
         self.next_token.set(mio::Token(token.0 + 1));
 
-        self.poll.borrow().registry().register(
-            &mut mio::unix::SourceFd(&fd),
-            token,
-            mio::Interest::READABLE,
-        ).expect("couldn't register initial readable interest?");
+        self.poll
+            .borrow()
+            .registry()
+            .register(
+                &mut mio::unix::SourceFd(&fd),
+                token,
+                mio::Interest::READABLE,
+            )
+            .expect("couldn't register initial readable interest?");
 
         InterestRegistration {
             poll: self.poll.clone(),
@@ -115,18 +126,22 @@ impl UdpSocketBuilder {
     }
 
     pub fn build(self, io: &IOService) {
-        let socket = mio::net::UdpSocket::bind(self.bind.expect("no bind address given to UDP socket!")).expect("couldn't bind UDP socket");
+        let socket =
+            mio::net::UdpSocket::bind(self.bind.expect("no bind address given to UDP socket!"))
+                .expect("couldn't bind UDP socket");
         let interest = io.register_interest(socket.as_raw_fd());
 
         for mcast in self.multicast {
             use std::net::IpAddr;
             match (mcast.0, mcast.1) {
                 (IpAddr::V4(group), IpAddr::V4(iface)) => {
-                    socket.join_multicast_v4(&group, &iface).expect("couldn't join multicast group");
-                },
+                    socket
+                        .join_multicast_v4(&group, &iface)
+                        .expect("couldn't join multicast group");
+                }
                 (IpAddr::V6(_group), IpAddr::V6(_iface)) => {
                     todo!()
-                },
+                }
                 _ => panic!("Multicast specification mixes ipv4 and ipv6 addresses!"),
             }
         }
@@ -153,13 +168,12 @@ impl UdpSocket {
     fn peer_for(&self, rc: &Rc<dyn FdHandler>, addr: &std::net::SocketAddr) -> super::peer::Peer {
         if let Some(peer) = self.peers.borrow().get(addr) {
             peer.clone()
-        }
-        else {
+        } else {
             let peer = super::peer::Peer {
                 data: Rc::new(super::peer::PeerData {
                     address_info: super::peer::PeerAddress::Udp(*addr),
                     io: rc.clone(),
-                })
+                }),
             };
             self.peers.borrow_mut().insert(*addr, peer.clone());
             peer
@@ -185,11 +199,14 @@ impl FdHandler for UdpSocket {
 
             if let Ok(len) = self.socket.send_to(&packet.1, packet.0) {
                 if len != packet.1.len() {
-                    log::error!("Sent packet truncated to {} of {} bytes", len, packet.1.len());
+                    log::error!(
+                        "Sent packet truncated to {} of {} bytes",
+                        len,
+                        packet.1.len()
+                    );
                 }
-            }
-            else {
-                return
+            } else {
+                return;
             }
         }
 
@@ -200,11 +217,13 @@ impl FdHandler for UdpSocket {
         let peer = msg.peer.clone().unwrap(); // if we got this far, the peer points to us
 
         if let super::peer::PeerAddress::Udp(addr) = peer.data.address_info {
-            self.queue.borrow_mut().push_back(
-                (addr, bincode::serialize(&msg).expect("couldn't serialize message?")));
-            self.interest.update_interest(mio::Interest::READABLE.add(mio::Interest::WRITABLE));
-        }
-        else {
+            self.queue.borrow_mut().push_back((
+                addr,
+                bincode::serialize(&msg).expect("couldn't serialize message?"),
+            ));
+            self.interest
+                .update_interest(mio::Interest::READABLE.add(mio::Interest::WRITABLE));
+        } else {
             log::error!("packet dispatched to UdpSocket without UDP address!");
         }
     }
@@ -230,10 +249,15 @@ impl TimerFdBuilder {
     pub fn build(self, io: &IOService) {
         let mut timer = timerfd::TimerFd::new().expect("couldn't create new timerfd?");
 
-        timer.set_state(timerfd::TimerState::Periodic {
-            current: std::time::Duration::from_millis(1),
-            interval: self.interval.expect("building timerfd with no interval set?"),
-        }, timerfd::SetTimeFlags::Default);
+        timer.set_state(
+            timerfd::TimerState::Periodic {
+                current: std::time::Duration::from_millis(1),
+                interval: self
+                    .interval
+                    .expect("building timerfd with no interval set?"),
+            },
+            timerfd::SetTimeFlags::Default,
+        );
 
         let interest = io.register_interest(timer.as_raw_fd());
 

+ 1 - 1
fleck/src/fleck_core/msg.rs

@@ -68,7 +68,7 @@ impl Message {
             data: bincode::serialize(&from).expect("couldn't serialize message"),
             saved_params: Some(SavedMessageParams::save::<M>()),
             parsed: Some(Box::new(from)),
-            peer: None
+            peer: None,
         }
     }
 

+ 14 - 9
fleck/src/fleck_core/peer.rs

@@ -1,6 +1,6 @@
 //! Peer: someone we can communicate with directly
 
-use std::{rc::Rc, collections::HashMap, cell::RefCell};
+use std::{cell::RefCell, collections::HashMap, rc::Rc};
 
 use crate::prelude::*;
 
@@ -9,7 +9,7 @@ use super::msg::Message;
 pub(crate) enum PeerAddress {
     Stream,
     Udp(std::net::SocketAddr),
-    Virtual
+    Virtual,
 }
 
 pub(crate) struct PeerData {
@@ -23,26 +23,31 @@ impl std::fmt::Debug for PeerData {
     }
 }
 
-#[derive(Clone,Debug)]
+#[derive(Clone, Debug)]
 pub struct Peer {
-    pub(crate) data: Rc<PeerData>
+    pub(crate) data: Rc<PeerData>,
 }
 
 #[derive(Default)]
-pub struct PeerService {
-}
+pub struct PeerService {}
 
 impl Service for std::rc::Rc<PeerService> {
     fn setup(&self, api: &crate::Fleck) {
-        api.channel::<fleck_core::SendPacketChannel>().as_ref().sub_opt(fleck_core::SendOrder::Send, self, PeerService::dispatch);
+        api.channel::<fleck_core::SendPacketChannel>()
+            .as_ref()
+            .sub_opt(fleck_core::SendOrder::Send, self, PeerService::dispatch);
     }
 }
 
 impl PeerService {
-    fn dispatch(&self, api: &crate::Fleck, msg: super::msg::Message) -> Option<super::msg::Message> {
+    fn dispatch(
+        &self,
+        api: &crate::Fleck,
+        msg: super::msg::Message,
+    ) -> Option<super::msg::Message> {
         if msg.peer.is_none() {
             log::error!("Message got to dispatch with no peer selected?");
-            return None
+            return None;
         }
         let peer = msg.peer.clone().unwrap();
         peer.data.io.dispatch(api, msg);

+ 7 - 3
fleck/src/lib.rs

@@ -6,9 +6,9 @@ mod helper;
 pub mod service;
 
 pub mod prelude {
+    pub use crate::fleck_core;
     pub use crate::helper::{AsAny, IntoWeak};
     pub use crate::service::{ChannelSpec, EventRoot, Service};
-    pub use crate::fleck_core;
 }
 
 use prelude::*;
@@ -69,12 +69,16 @@ impl Fleck {
         self.services.with_service(f)
     }
     /// Create an event channel from a specification
-    pub fn create_channel<CS: service::ChannelSpec>(&self) -> Rc<service::event::Channel<CS::Tag, Self, CS::Data, CS::Priority>> {
+    pub fn create_channel<CS: service::ChannelSpec>(
+        &self,
+    ) -> Rc<service::event::Channel<CS::Tag, Self, CS::Data, CS::Priority>> {
         self.services.event_root().create_channel::<CS>();
         self.channel::<CS>()
     }
     /// Get an existing channel
-    pub fn channel<CS: service::ChannelSpec>(&self) -> Rc<service::event::Channel<CS::Tag, Self, CS::Data, CS::Priority>> {
+    pub fn channel<CS: service::ChannelSpec>(
+        &self,
+    ) -> Rc<service::event::Channel<CS::Tag, Self, CS::Data, CS::Priority>> {
         self.services.event_root().channel::<CS>()
     }
     /// Queue a message onto a channel

+ 10 - 4
fleck/src/service.rs

@@ -1,8 +1,8 @@
 use std::any::TypeId;
+use std::cell::RefCell;
 use std::collections::HashMap;
 use std::ops::Deref;
 use std::rc::Rc;
-use std::cell::RefCell;
 
 pub(crate) mod event;
 
@@ -51,7 +51,8 @@ impl ServiceStack {
     {
         srv.setup(api);
 
-        self.service_map.borrow_mut()
+        self.service_map
+            .borrow_mut()
             .insert(TypeId::of::<S>(), Box::new(srv.clone()));
         self.services.borrow_mut().push(Box::new(srv));
     }
@@ -64,8 +65,13 @@ impl ServiceStack {
 
         let svc = {
             let svcs = self.service_map.borrow();
-            svcs.get(&id).expect("asked for service that doesn't exist!")
-                .deref().as_any().downcast_ref::<Rc<S>>().unwrap().clone()
+            svcs.get(&id)
+                .expect("asked for service that doesn't exist!")
+                .deref()
+                .as_any()
+                .downcast_ref::<Rc<S>>()
+                .unwrap()
+                .clone()
         };
 
         f(svc.as_ref())

+ 9 - 5
fleck/src/service/event.rs

@@ -239,12 +239,15 @@ impl<Context: 'static + ?Sized> EventRoot<Context> {
         let tid = std::any::TypeId::of::<Channel<CS::Tag, Context, CS::Data, CS::Priority>>();
 
         let ch = Rc::new(Channel::<CS::Tag, Context, CS::Data, CS::Priority>::default());
-        self.metadata.borrow_mut()
+        self.metadata
+            .borrow_mut()
             .insert(tid, (std::any::type_name::<CS>(), ch.clone()));
         self.channels.borrow_mut().insert(tid, ch);
     }
 
-    pub fn channel<'l, 's: 'l, CS: ChannelSpec>(&'s self) -> Rc<Channel<CS::Tag, Context, CS::Data, CS::Priority>> {
+    pub fn channel<'l, 's: 'l, CS: ChannelSpec>(
+        &'s self,
+    ) -> Rc<Channel<CS::Tag, Context, CS::Data, CS::Priority>> {
         let tid = std::any::TypeId::of::<Channel<CS::Tag, Context, CS::Data, CS::Priority>>();
 
         let own = self.channels.borrow();
@@ -252,9 +255,10 @@ impl<Context: 'static + ?Sized> EventRoot<Context> {
         if own.contains_key(&tid) {
             let entry = own.get(&tid).unwrap().clone();
 
-            entry.downcast::<Channel<CS::Tag, Context, CS::Data, CS::Priority>>().expect("internal inconsistency?")
-        }
-        else {
+            entry
+                .downcast::<Channel<CS::Tag, Context, CS::Data, CS::Priority>>()
+                .expect("internal inconsistency?")
+        } else {
             panic!(
                 "Asked for channel {} that has not been created!",
                 std::any::type_name::<CS>()