From 6df6411b7234c36555e5b38aa7084f641354cce2 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 31 Jan 2023 23:57:33 +0100 Subject: fix clippy --- examples/fullmesh.rs | 4 ++-- src/bytes_buf.rs | 59 +++++++++++++++++++++++++++++-------------------- src/client.rs | 4 ++-- src/message.rs | 10 ++++----- src/peering/fullmesh.rs | 2 +- src/server.rs | 2 +- src/util.rs | 6 ++--- 7 files changed, 49 insertions(+), 38 deletions(-) diff --git a/examples/fullmesh.rs b/examples/fullmesh.rs index d0190ef..5bbde73 100644 --- a/examples/fullmesh.rs +++ b/examples/fullmesh.rs @@ -72,7 +72,7 @@ async fn main() { }; info!("Node private key: {}", hex::encode(&privkey)); - info!("Node public key: {}", hex::encode(&privkey.public_key())); + info!("Node public key: {}", hex::encode(privkey.public_key())); let public_addr = opt.public_addr.map(|x| x.parse().unwrap()); let listen_addr: SocketAddr = opt.listen_addr.parse().unwrap(); @@ -94,7 +94,7 @@ async fn main() { info!("Add more peers to this mesh by running: fullmesh -n {} -l 127.0.0.1:$((1000 + $RANDOM)) -b {}@{}", hex::encode(&netid), - hex::encode(&privkey.public_key()), + hex::encode(privkey.public_key()), listen_addr); let watch_cancel = netapp::util::watch_ctrl_c(); diff --git a/src/bytes_buf.rs b/src/bytes_buf.rs index 931be82..3929a86 100644 --- a/src/bytes_buf.rs +++ b/src/bytes_buf.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::collections::VecDeque; use bytes::BytesMut; @@ -44,7 +45,7 @@ impl BytesBuf { /// Takes the whole content of the buffer and returns it as a single Bytes unit pub fn take_all(&mut self) -> Bytes { - if self.buf.len() == 0 { + if self.buf.is_empty() { Bytes::new() } else if self.buf.len() == 1 { self.buf_len = 0; @@ -82,31 +83,35 @@ impl BytesBuf { fn take_exact_ok(&mut self, len: usize) -> Bytes { assert!(len <= self.buf_len); let front = self.buf.pop_front().unwrap(); - if front.len() > len { - self.buf.push_front(front.slice(len..)); - self.buf_len -= len; - front.slice(..len) - } else if front.len() == len { - self.buf_len -= len; - front - } else { - let mut ret = BytesMut::with_capacity(len); - ret.extend_from_slice(&front[..]); - self.buf_len -= front.len(); - while ret.len() < len { - let front = self.buf.pop_front().unwrap(); - if front.len() > len - ret.len() { - let take = len - ret.len(); - ret.extend_from_slice(&front[..take]); - self.buf.push_front(front.slice(take..)); - self.buf_len -= take; - break; - } else { - ret.extend_from_slice(&front[..]); - self.buf_len -= front.len(); + match front.len().cmp(&len) { + Ordering::Greater => { + self.buf.push_front(front.slice(len..)); + self.buf_len -= len; + front.slice(..len) + } + Ordering::Equal => { + self.buf_len -= len; + front + } + Ordering::Less => { + let mut ret = BytesMut::with_capacity(len); + ret.extend_from_slice(&front[..]); + self.buf_len -= front.len(); + while ret.len() < len { + let front = self.buf.pop_front().unwrap(); + if front.len() > len - ret.len() { + let take = len - ret.len(); + ret.extend_from_slice(&front[..take]); + self.buf.push_front(front.slice(take..)); + self.buf_len -= take; + break; + } else { + ret.extend_from_slice(&front[..]); + self.buf_len -= front.len(); + } } + ret.freeze() } - ret.freeze() } } @@ -116,6 +121,12 @@ impl BytesBuf { } } +impl Default for BytesBuf { + fn default() -> Self { + Self::new() + } +} + impl From for BytesBuf { fn from(b: Bytes) -> BytesBuf { let mut ret = BytesBuf::new(); diff --git a/src/client.rs b/src/client.rs index d82c91e..607dd17 100644 --- a/src/client.rs +++ b/src/client.rs @@ -65,7 +65,7 @@ impl ClientConn { debug!( "Handshake complete (client) with {}@{}", - hex::encode(&peer_id), + hex::encode(peer_id), remote_addr ); @@ -250,7 +250,7 @@ impl CancelOnDrop { fn for_stream(self, stream: ByteStream) -> CancelOnDropStream { CancelOnDropStream { cancel: Some(self), - stream: stream, + stream, } } } diff --git a/src/message.rs b/src/message.rs index 9cc1a3e..cd40ea7 100644 --- a/src/message.rs +++ b/src/message.rs @@ -54,11 +54,11 @@ pub struct OrderTagStream(u64); impl OrderTag { /// Create a new stream from which to generate order tags. Example: - /// ```ignore - /// let stream = OrderTag.stream(); - /// let tag_1 = stream.order(1); - /// let tag_2 = stream.order(2); - /// ``` + /// ```ignore + /// let stream = OrderTag.stream(); + /// let tag_1 = stream.order(1); + /// let tag_2 = stream.order(2); + /// ``` pub fn stream() -> OrderTagStream { OrderTagStream(thread_rng().gen()) } diff --git a/src/peering/fullmesh.rs b/src/peering/fullmesh.rs index 859a94a..8e66604 100644 --- a/src/peering/fullmesh.rs +++ b/src/peering/fullmesh.rs @@ -113,7 +113,7 @@ impl PeerInfo { /// PeerConnState: possible states for our tentative connections to given peer /// This structure is only interested in recording connection info for outgoing /// TCP connections -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum PeerConnState { /// This entry represents ourself (the local node) Ourself, diff --git a/src/server.rs b/src/server.rs index cd367c4..55b9e67 100644 --- a/src/server.rs +++ b/src/server.rs @@ -79,7 +79,7 @@ impl ServerConn { debug!( "Handshake complete (server) with {}@{}", - hex::encode(&peer_id), + hex::encode(peer_id), remote_addr ); diff --git a/src/util.rs b/src/util.rs index 4676e64..edf8c49 100644 --- a/src/util.rs +++ b/src/util.rs @@ -62,7 +62,7 @@ pub fn watch_ctrl_c() -> watch::Receiver { pub fn parse_peer_addr(peer: &str) -> Option<(NodeID, SocketAddr)> { let delim = peer.find('@')?; let (key, ip) = peer.split_at(delim); - let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?; + let pubkey = NodeID::from_slice(&hex::decode(key).ok()?)?; let ip = ip[1..].parse::().ok()?; Some((pubkey, ip)) } @@ -74,7 +74,7 @@ pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec>(); if hosts.is_empty() { return None; @@ -86,7 +86,7 @@ pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec Option<(NodeID, Vec)> { let delim = peer.find('@')?; let (key, host) = peer.split_at(delim); - let pubkey = NodeID::from_slice(&hex::decode(&key).ok()?)?; + let pubkey = NodeID::from_slice(&hex::decode(key).ok()?)?; let hosts = tokio::net::lookup_host(&host[1..]) .await .ok()? -- cgit v1.2.3