]> git.lizzy.rs Git - connect-rs.git/commitdiff
remove unnecessary copies of local and peer addrs
authorSachandhan Ganesh <sachan.ganesh@gmail.com>
Sat, 13 Feb 2021 07:36:02 +0000 (23:36 -0800)
committerSachandhan Ganesh <sachan.ganesh@gmail.com>
Sat, 13 Feb 2021 07:36:02 +0000 (23:36 -0800)
Cargo.toml
src/lib.rs
src/reader.rs
src/tcp/client.rs
src/tls/client.rs
src/writer.rs

index 999615a5e5931b3773f44f4f353fac01903b809c..2cf5a0f2262906fb88073a440e19cbda7af02d5b 100644 (file)
@@ -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"
index 45f76da53ee4caff795accb05202e55d90c82df5..1a0cb2bc185d3edb91b512036a59b8b7fe91f0ac 100644 (file)
 //! 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<Box<dyn AsyncWrite + Send + Sync>>,
     ) -> 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();
index 39b1048f896fca459f491d4b566cf74c4acd11ad..b046c84bdd1e5429ada023bb14fac654ebc78b00 100644 (file)
@@ -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
     }
index 28ae2a3a54f2462ed530650d2cf9ac48ae032efb..a02f3796cf9c8f8a9ae9e539f2bb2d98e71a75db 100644 (file)
@@ -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
     ///
index 1fa8d08fce43b90eb0276b5d8d9c936732c63307..33a62977d842b96f81ad711d677413f1ffde058b 100644 (file)
@@ -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
     ///
index 4ee98f29456002ee9df3d1ebce11b605b0fece6f..b8c8a95f6ccb403f399cc61262ec294ed680b557 100644 (file)
@@ -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
     }