From 1e8799cf4afa592aad81905698cb0a7ec8be9e77 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 22 May 2021 22:01:40 +0900 Subject: [PATCH] Remove boxed futures from TcpStream and UnixStream --- Cargo.toml | 2 +- src/tcp.rs | 24 ++++++++++-------------- src/unix.rs | 19 ++++++++----------- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 992c5b6..38f404a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["networking", "uds", "mio", "reactor", "std"] categories = ["asynchronous", "network-programming", "os"] [dependencies] -async-io = "1.0.0" +async-io = "1.5.0" blocking = "1.0.0" fastrand = "1.3.5" futures-lite = "1.11.0" diff --git a/src/tcp.rs b/src/tcp.rs index ef45308..610c3a8 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -303,8 +303,8 @@ impl fmt::Debug for Incoming<'_> { /// ``` pub struct TcpStream { inner: Arc>, - readable: Option> + Send + Sync>>>, - writable: Option> + Send + Sync>>>, + readable: Option, + writable: Option, } impl UnwindSafe for TcpStream {} @@ -599,13 +599,12 @@ impl AsyncRead for TcpStream { // Initialize the future to wait for readiness. if self.readable.is_none() { - let inner = self.inner.clone(); - self.readable = Some(Box::pin(async move { inner.readable().await })); + self.readable = Some(self.inner.readable()); } // Poll the future for readiness. if let Some(f) = &mut self.readable { - let res = ready!(f.as_mut().poll(cx)); + let res = ready!(Pin::new(f).poll(cx)); self.readable = None; res?; } @@ -631,13 +630,12 @@ impl AsyncWrite for TcpStream { // Initialize the future to wait for readiness. if self.writable.is_none() { - let inner = self.inner.clone(); - self.writable = Some(Box::pin(async move { inner.writable().await })); + self.writable = Some(self.inner.writable()); } // Poll the future for readiness. if let Some(f) = &mut self.writable { - let res = ready!(f.as_mut().poll(cx)); + let res = ready!(Pin::new(f).poll(cx)); self.writable = None; res?; } @@ -657,13 +655,12 @@ impl AsyncWrite for TcpStream { // Initialize the future to wait for readiness. if self.writable.is_none() { - let inner = self.inner.clone(); - self.writable = Some(Box::pin(async move { inner.writable().await })); + self.writable = Some(self.inner.writable()); } // Poll the future for readiness. if let Some(f) = &mut self.writable { - let res = ready!(f.as_mut().poll(cx)); + let res = ready!(Pin::new(f).poll(cx)); self.writable = None; res?; } @@ -691,13 +688,12 @@ impl AsyncWrite for TcpStream { // Initialize the future to wait for readiness. if self.writable.is_none() { - let inner = self.inner.clone(); - self.writable = Some(Box::pin(async move { inner.writable().await })); + self.writable = Some(self.inner.writable()); } // Poll the future for readiness. if let Some(f) = &mut self.writable { - let res = ready!(f.as_mut().poll(cx)); + let res = ready!(Pin::new(f).poll(cx)); self.writable = None; res?; } diff --git a/src/unix.rs b/src/unix.rs index 928c7a4..7956c9e 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -234,8 +234,8 @@ impl fmt::Debug for Incoming<'_> { /// ``` pub struct UnixStream { inner: Arc>, - readable: Option> + Send + Sync>>>, - writable: Option> + Send + Sync>>>, + readable: Option, + writable: Option, } impl UnwindSafe for UnixStream {} @@ -396,13 +396,12 @@ impl AsyncRead for UnixStream { // Initialize the future to wait for readiness. if self.readable.is_none() { - let inner = self.inner.clone(); - self.readable = Some(Box::pin(async move { inner.readable().await })); + self.readable = Some(self.inner.readable()); } // Poll the future for readiness. if let Some(f) = &mut self.readable { - let res = ready!(f.as_mut().poll(cx)); + let res = ready!(Pin::new(f).poll(cx)); self.readable = None; res?; } @@ -428,13 +427,12 @@ impl AsyncWrite for UnixStream { // Initialize the future to wait for readiness. if self.writable.is_none() { - let inner = self.inner.clone(); - self.writable = Some(Box::pin(async move { inner.writable().await })); + self.writable = Some(self.inner.writable()); } // Poll the future for readiness. if let Some(f) = &mut self.writable { - let res = ready!(f.as_mut().poll(cx)); + let res = ready!(Pin::new(f).poll(cx)); self.writable = None; res?; } @@ -454,13 +452,12 @@ impl AsyncWrite for UnixStream { // Initialize the future to wait for readiness. if self.writable.is_none() { - let inner = self.inner.clone(); - self.writable = Some(Box::pin(async move { inner.writable().await })); + self.writable = Some(self.inner.writable()); } // Poll the future for readiness. if let Some(f) = &mut self.writable { - let res = ready!(f.as_mut().poll(cx)); + let res = ready!(Pin::new(f).poll(cx)); self.writable = None; res?; }