From 1b70507894035e066cb488c14ff328bd47ca696d Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Sat, 19 Dec 2020 21:17:52 +0100 Subject: [PATCH] net: remove {Tcp,Unix}Stream::shutdown() (#3298) `shutdown()` on `AsyncWrite` performs a TCP shutdown. This avoids method conflicts. Closes #3294 --- tokio/src/net/tcp/split.rs | 2 +- tokio/src/net/tcp/split_owned.rs | 4 ++-- tokio/src/net/tcp/stream.rs | 34 ++++++++++--------------------- tokio/src/net/unix/split.rs | 2 +- tokio/src/net/unix/split_owned.rs | 4 ++-- tokio/src/net/unix/stream.rs | 15 ++++++++++---- 6 files changed, 28 insertions(+), 33 deletions(-) diff --git a/tokio/src/net/tcp/split.rs b/tokio/src/net/tcp/split.rs index f1d71a6d8..891b90d02 100644 --- a/tokio/src/net/tcp/split.rs +++ b/tokio/src/net/tcp/split.rs @@ -171,7 +171,7 @@ impl AsyncWrite for WriteHalf<'_> { // `poll_shutdown` on a write half shutdowns the stream in the "write" direction. fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { - self.0.shutdown(Shutdown::Write).into() + self.0.shutdown_std(Shutdown::Write).into() } } diff --git a/tokio/src/net/tcp/split_owned.rs b/tokio/src/net/tcp/split_owned.rs index 65fe2223e..3e5bcf12b 100644 --- a/tokio/src/net/tcp/split_owned.rs +++ b/tokio/src/net/tcp/split_owned.rs @@ -219,7 +219,7 @@ impl OwnedWriteHalf { impl Drop for OwnedWriteHalf { fn drop(&mut self) { if self.shutdown_on_drop { - let _ = self.inner.shutdown(Shutdown::Write); + let _ = self.inner.shutdown_std(Shutdown::Write); } } } @@ -253,7 +253,7 @@ impl AsyncWrite for OwnedWriteHalf { // `poll_shutdown` on a write half shutdowns the stream in the "write" direction. fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { - let res = self.inner.shutdown(Shutdown::Write); + let res = self.inner.shutdown_std(Shutdown::Write); if res.is_ok() { Pin::into_inner(self).shutdown_on_drop = false; } diff --git a/tokio/src/net/tcp/stream.rs b/tokio/src/net/tcp/stream.rs index a2bba6311..9195d3e8f 100644 --- a/tokio/src/net/tcp/stream.rs +++ b/tokio/src/net/tcp/stream.rs @@ -56,6 +56,13 @@ cfg_net! { /// /// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt + /// + /// To shut down the stream in the write direction, you can call the + /// [`shutdown()`] method. This will cause the other peer to receive a read of + /// length 0, indicating that no more data will be sent. This only closes + /// the stream in one direction. + /// + /// [`shutdown()`]: fn@crate::io::AsyncWriteExt::shutdown pub struct TcpStream { io: PollEvented, } @@ -685,26 +692,7 @@ impl TcpStream { /// This function will cause all pending and future I/O on the specified /// portions to return immediately with an appropriate value (see the /// documentation of `Shutdown`). - /// - /// # Examples - /// - /// ```no_run - /// use tokio::net::TcpStream; - /// use std::error::Error; - /// use std::net::Shutdown; - /// - /// #[tokio::main] - /// async fn main() -> Result<(), Box> { - /// // Connect to a peer - /// let stream = TcpStream::connect("127.0.0.1:8080").await?; - /// - /// // Shutdown the stream - /// stream.shutdown(Shutdown::Write)?; - /// - /// Ok(()) - /// } - /// ``` - pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { + pub(super) fn shutdown_std(&self, how: Shutdown) -> io::Result<()> { self.io.shutdown(how) } @@ -882,10 +870,10 @@ impl TcpStream { /// this comes at the cost of a heap allocation. /// /// **Note:** Dropping the write half will shut down the write half of the TCP - /// stream. This is equivalent to calling [`shutdown(Write)`] on the `TcpStream`. + /// stream. This is equivalent to calling [`shutdown()`] on the `TcpStream`. /// /// [`split`]: TcpStream::split() - /// [`shutdown(Write)`]: fn@crate::net::TcpStream::shutdown + /// [`shutdown()`]: fn@crate::io::AsyncWriteExt::shutdown pub fn into_split(self) -> (OwnedReadHalf, OwnedWriteHalf) { split_owned(self) } @@ -979,7 +967,7 @@ impl AsyncWrite for TcpStream { } fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { - self.shutdown(std::net::Shutdown::Write)?; + self.shutdown_std(std::net::Shutdown::Write)?; Poll::Ready(Ok(())) } } diff --git a/tokio/src/net/unix/split.rs b/tokio/src/net/unix/split.rs index 46849e369..24a711bbd 100644 --- a/tokio/src/net/unix/split.rs +++ b/tokio/src/net/unix/split.rs @@ -83,7 +83,7 @@ impl AsyncWrite for WriteHalf<'_> { } fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { - self.0.shutdown(Shutdown::Write).into() + self.0.shutdown_std(Shutdown::Write).into() } } diff --git a/tokio/src/net/unix/split_owned.rs b/tokio/src/net/unix/split_owned.rs index f5a93837a..3d6ac6a7e 100644 --- a/tokio/src/net/unix/split_owned.rs +++ b/tokio/src/net/unix/split_owned.rs @@ -136,7 +136,7 @@ impl OwnedWriteHalf { impl Drop for OwnedWriteHalf { fn drop(&mut self) { if self.shutdown_on_drop { - let _ = self.inner.shutdown(Shutdown::Write); + let _ = self.inner.shutdown_std(Shutdown::Write); } } } @@ -170,7 +170,7 @@ impl AsyncWrite for OwnedWriteHalf { // `poll_shutdown` on a write half shutdowns the stream in the "write" direction. fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { - let res = self.inner.shutdown(Shutdown::Write); + let res = self.inner.shutdown_std(Shutdown::Write); if res.is_ok() { Pin::into_inner(self).shutdown_on_drop = false; } diff --git a/tokio/src/net/unix/stream.rs b/tokio/src/net/unix/stream.rs index e80c9217a..886d78f62 100644 --- a/tokio/src/net/unix/stream.rs +++ b/tokio/src/net/unix/stream.rs @@ -21,6 +21,13 @@ cfg_net_unix! { /// This socket can be connected directly with `UnixStream::connect` or accepted /// from a listener with `UnixListener::incoming`. Additionally, a pair of /// anonymous Unix sockets can be created with `UnixStream::pair`. + /// + /// To shut down the stream in the write direction, you can call the + /// [`shutdown()`] method. This will cause the other peer to receive a read of + /// length 0, indicating that no more data will be sent. This only closes + /// the stream in one direction. + /// + /// [`shutdown()`]: fn@crate::io::AsyncWriteExt::shutdown pub struct UnixStream { io: PollEvented, } @@ -415,7 +422,7 @@ impl UnixStream { /// This function will cause all pending and future I/O calls on the /// specified portions to immediately return with an appropriate value /// (see the documentation of `Shutdown`). - pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { + pub(super) fn shutdown_std(&self, how: Shutdown) -> io::Result<()> { self.io.shutdown(how) } @@ -440,10 +447,10 @@ impl UnixStream { /// this comes at the cost of a heap allocation. /// /// **Note:** Dropping the write half will shut down the write half of the - /// stream. This is equivalent to calling [`shutdown(Write)`] on the `UnixStream`. + /// stream. This is equivalent to calling [`shutdown()`] on the `UnixStream`. /// /// [`split`]: Self::split() - /// [`shutdown(Write)`]: fn@Self::shutdown + /// [`shutdown()`]: fn@crate::io::AsyncWriteExt::shutdown pub fn into_split(self) -> (OwnedReadHalf, OwnedWriteHalf) { split_owned(self) } @@ -497,7 +504,7 @@ impl AsyncWrite for UnixStream { } fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { - self.shutdown(std::net::Shutdown::Write)?; + self.shutdown_std(std::net::Shutdown::Write)?; Poll::Ready(Ok(())) } }