diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bytes_buf.rs | 59 | ||||
-rw-r--r-- | src/client.rs | 4 | ||||
-rw-r--r-- | src/message.rs | 10 | ||||
-rw-r--r-- | src/peering/fullmesh.rs | 2 | ||||
-rw-r--r-- | src/server.rs | 2 | ||||
-rw-r--r-- | src/util.rs | 6 |
6 files changed, 47 insertions, 36 deletions
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<Bytes> 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<bool> { 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::<SocketAddr>().ok()?; Some((pubkey, ip)) } @@ -74,7 +74,7 @@ pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr 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 = host[1..].to_socket_addrs().ok()?.collect::<Vec<_>>(); if hosts.is_empty() { return None; @@ -86,7 +86,7 @@ pub fn parse_and_resolve_peer_addr(peer: &str) -> Option<(NodeID, Vec<SocketAddr pub async fn parse_and_resolve_peer_addr_async(peer: &str) -> Option<(NodeID, Vec<SocketAddr>)> { 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()? |