diff --git a/tokio/src/net/tcp/split.rs b/tokio/src/net/tcp/split.rs index 296b469d4a3..605029c759d 100644 --- a/tokio/src/net/tcp/split.rs +++ b/tokio/src/net/tcp/split.rs @@ -173,7 +173,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 725d7411ea0..cd66903a7a1 100644 --- a/tokio/src/net/tcp/split_owned.rs +++ b/tokio/src/net/tcp/split_owned.rs @@ -221,7 +221,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); } } } @@ -255,7 +255,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 56948ffa66e..5239fce4046 100644 --- a/tokio/src/net/tcp/stream.rs +++ b/tokio/src/net/tcp/stream.rs @@ -57,6 +57,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, } @@ -686,26 +693,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) } @@ -883,10 +871,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) } @@ -980,7 +968,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 af9c762494e..c9fa94d9176 100644 --- a/tokio/src/net/unix/split.rs +++ b/tokio/src/net/unix/split.rs @@ -85,7 +85,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 5f0a2593d01..f121c0a711e 100644 --- a/tokio/src/net/unix/split_owned.rs +++ b/tokio/src/net/unix/split_owned.rs @@ -139,7 +139,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); } } } @@ -173,7 +173,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 e80c9217af2..886d78f62ed 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(())) } }