From: Sachandhan Ganesh Date: Sat, 13 Feb 2021 07:36:02 +0000 (-0800) Subject: remove unnecessary copies of local and peer addrs X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=1d41d1f71b5b457236c6cfbc11111e6bebe20aec;p=connect-rs.git remove unnecessary copies of local and peer addrs --- diff --git a/Cargo.toml b/Cargo.toml index 999615a..2cf5a0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ members = [ [dependencies] anyhow = "1.0.31" async-channel = "1.4.0" -async-std = { version = "1.6.2", features = ["attributes", "unstable"] } +async-std = { version = "1.9.0", features = ["attributes", "unstable"] } async-tls = { version = "0.9.0", default-features = false, features = ["client", "server"]} bytes = "0.5.5" futures = "0.3.8" diff --git a/src/lib.rs b/src/lib.rs index 45f76da..1a0cb2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,20 +11,8 @@ //! Please use the [examples](https://github.com/sachanganesh/connect-rs/tree/main/examples) //! provided to help understand crate usage. //! -//! # Protobuf -//! This crate relies on the use of [Protocol Buffers](https://developers.google.com/protocol-buffers) -//! due to it being widely adopted and industry-proven. All messages are Protobuf messages that -//! are packed into a Protobuf `Any` type and then sent over the wire. Message recipients must -//! decide what Protobuf message type it is, and correspondingly unpack the `Any` into a particular -//! message type. -//! -//! Protobuf was chosen when the library hit a roadblock with Rust's `TypeId` potentially not being -//! consistent between Rust compiler versions. The crate requires a consistent way to determine what -//! type of message is received, so it can appropriately deserialize the message from network bytes. -//! Until the Rust ecosystem around reflection improves, the crate will use Protobuf to fill the -//! void. -pub mod protocol; +mod protocol; mod reader; pub mod tcp; pub mod tls; @@ -38,10 +26,8 @@ use futures::{AsyncRead, AsyncWrite}; pub use futures::{SinkExt, StreamExt}; /// Wrapper around a [`ConnectionReader`] and [`ConnectionWriter`] to read and write on a network -/// connection +/// connection. pub struct Connection { - local_addr: SocketAddr, - peer_addr: SocketAddr, reader: ConnectionReader, writer: ConnectionWriter, } @@ -55,52 +41,45 @@ impl Connection { write_stream: Pin>, ) -> Self { Self { - local_addr, - peer_addr, reader: ConnectionReader::new(local_addr, peer_addr, read_stream), writer: ConnectionWriter::new(local_addr, peer_addr, write_stream), } } - /// Get the local IP address and port + /// Get the local IP address and port. pub fn local_addr(&self) -> SocketAddr { - self.local_addr.clone() + self.reader.local_addr() } - /// Get the peer IP address and port + /// Get the peer IP address and port. pub fn peer_addr(&self) -> SocketAddr { - self.peer_addr.clone() + self.reader.peer_addr() } /// Consume the [`Connection`] to split into separate [`ConnectionReader`] and - /// [`ConnectionWriter`] halves + /// [`ConnectionWriter`] halves. /// /// [`Connection`]s are split when reading and writing must be concurrent operations. pub fn split(self) -> (ConnectionReader, ConnectionWriter) { (self.reader, self.writer) } - /// Re-wrap the [`ConnectionReader`] and [`ConnectionWriter`] halves into a [`Connection`] + /// Re-wrap the [`ConnectionReader`] and [`ConnectionWriter`] halves into a [`Connection`]. pub fn join(reader: ConnectionReader, writer: ConnectionWriter) -> Self { - Self { - local_addr: reader.local_addr(), - peer_addr: reader.peer_addr(), - reader, - writer, - } + Self { reader, writer } } - /// Get mutable access to the underlying [`ConnectionReader`] + /// Get mutable access to the underlying [`ConnectionReader`]. pub fn reader(&mut self) -> &mut ConnectionReader { &mut self.reader } - /// Get mutable access to the underlying [`ConnectionWriter`] + /// Get mutable access to the underlying [`ConnectionWriter`]. pub fn writer(&mut self) -> &mut ConnectionWriter { &mut self.writer } - /// Close the connection by closing both the reading and writing halves + /// Close the connection by closing both the reading and writing halves. pub async fn close(self) -> SocketAddr { let peer_addr = self.peer_addr(); let (reader, writer) = self.split(); diff --git a/src/reader.rs b/src/reader.rs index 39b1048..b046c84 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -10,10 +10,10 @@ pub use futures::SinkExt; pub use futures::StreamExt; use std::io::Cursor; -/// A default buffer size to read in bytes and then deserialize as messages +/// A default buffer size to read in bytes and then deserialize as messages. const BUFFER_SIZE: usize = 8192; -/// An interface to read messages from the network connection +/// An interface to read messages from the network connection. /// /// Implements the [`Stream`] trait to asynchronously read messages from the network connection. /// @@ -41,7 +41,7 @@ pub struct ConnectionReader { impl ConnectionReader { /// Creates a new [`ConnectionReader`] from an [`AsyncRead`] trait object and the local and peer - /// socket metadata + /// socket metadata. pub fn new( local_addr: SocketAddr, peer_addr: SocketAddr, @@ -56,17 +56,17 @@ impl ConnectionReader { } } - /// Get the local IP address and port + /// Get the local IP address and port. pub fn local_addr(&self) -> SocketAddr { self.local_addr.clone() } - /// Get the peer IP address and port + /// Get the peer IP address and port. pub fn peer_addr(&self) -> SocketAddr { self.peer_addr.clone() } - /// Check if the [`Stream`] of messages from the network is closed + /// Check if the [`Stream`] of messages from the network is closed. pub fn is_closed(&self) -> bool { self.closed } diff --git a/src/tcp/client.rs b/src/tcp/client.rs index 28ae2a3..a02f379 100644 --- a/src/tcp/client.rs +++ b/src/tcp/client.rs @@ -4,7 +4,7 @@ use crate::Connection; use async_std::net::{TcpStream, ToSocketAddrs}; impl Connection { - /// Creates a [`Connection`] that uses a TCP transport + /// Creates a [`Connection`] that uses a TCP transport. /// /// # Example /// diff --git a/src/tls/client.rs b/src/tls/client.rs index 1fa8d08..33a6297 100644 --- a/src/tls/client.rs +++ b/src/tls/client.rs @@ -8,7 +8,7 @@ use crate::tls::TlsConnectionMetadata; use crate::Connection; impl Connection { - /// Creates a [`Connection`] that uses a TLS transport + /// Creates a [`Connection`] that uses a TLS transport. /// /// # Example /// diff --git a/src/writer.rs b/src/writer.rs index 4ee98f2..b8c8a95 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -10,7 +10,7 @@ use crate::protocol::ConnectDatagram; pub use futures::SinkExt; pub use futures::StreamExt; -/// An interface to write messages to the network connection +/// An interface to write messages to the network connection. /// /// Implements the [`Sink`] trait to asynchronously write messages to the network connection. /// @@ -35,7 +35,7 @@ pub struct ConnectionWriter { impl ConnectionWriter { /// Creates a new [`ConnectionWriter`] from an [`AsyncWrite`] trait object and the local and peer - /// socket metadata + /// socket metadata. pub fn new( local_addr: SocketAddr, peer_addr: SocketAddr, @@ -50,17 +50,17 @@ impl ConnectionWriter { } } - /// Get the local IP address and port + /// Get the local IP address and port. pub fn local_addr(&self) -> SocketAddr { self.local_addr.clone() } - /// Get the peer IP address and port + /// Get the peer IP address and port. pub fn peer_addr(&self) -> SocketAddr { self.peer_addr.clone() } - /// Check if the [`Sink`] of messages to the network is closed + /// Check if the [`Sink`] of messages to the network is closed. pub fn is_closed(&self) -> bool { self.closed }