]> git.lizzy.rs Git - connect-rs.git/blobdiff - src/tcp/client.rs
remove `block_on` in tls-listener
[connect-rs.git] / src / tcp / client.rs
index 140c00d4fff8c63058323a20c825c5e4d07f165e..d7f8ff8f6d9432f07158b6622ac0102fc694a5a5 100644 (file)
@@ -4,8 +4,22 @@ use crate::Connection;
 use async_std::net::{TcpStream, ToSocketAddrs};
 
 impl Connection {
-    pub fn tcp_client<A: ToSocketAddrs + std::fmt::Display>(ip_addrs: A) -> anyhow::Result<Self> {
-        let stream = futures::executor::block_on(TcpStream::connect(&ip_addrs))?;
+    /// Creates a [`Connection`] that uses a TCP transport.
+    ///
+    /// # Example
+    ///
+    /// Please see the [tcp-client](https://github.com/sachanganesh/connect-rs/blob/main/examples/tcp-client/src/main.rs)
+    /// example program for a more thorough showcase.
+    ///
+    /// Basic usage:
+    ///
+    /// ```ignore
+    /// let mut conn = Connection::tcp_client("127.0.0.1:3456").await?;
+    /// ```
+    pub async fn tcp_client<A: ToSocketAddrs + std::fmt::Display>(
+        ip_addrs: A,
+    ) -> anyhow::Result<Self> {
+        let stream = TcpStream::connect(&ip_addrs).await?;
         info!("Established client TCP connection to {}", ip_addrs);
 
         stream.set_nodelay(true)?;
@@ -14,6 +28,7 @@ impl Connection {
 }
 
 impl From<TcpStream> for Connection {
+    /// Creates a [`Connection`] using a TCP transport from an async [`TcpStream`].
     fn from(stream: TcpStream) -> Self {
         let write_stream = stream.clone();
 
@@ -28,8 +43,8 @@ impl From<TcpStream> for Connection {
         Self::new(
             local_addr,
             peer_addr,
-            Box::new(stream),
-            Box::new(write_stream),
+            Box::pin(stream),
+            Box::pin(write_stream),
         )
     }
 }