From 496efebf9bd65d821c4d079fc1f135d8a3e2067b Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 11 Dec 2024 19:38:53 -0500 Subject: [PATCH 1/2] impl `Read` and `Write` on `Arc` By analogy to #94748, this change makes `Arc` implement these traits that are already implemented on `&TcpStream`. --- library/std/src/net/tcp.rs | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/library/std/src/net/tcp.rs b/library/std/src/net/tcp.rs index 67a0f7e439d55..3bc5df3d3fc3e 100644 --- a/library/std/src/net/tcp.rs +++ b/library/std/src/net/tcp.rs @@ -15,6 +15,7 @@ use crate::io::prelude::*; use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut}; use crate::iter::FusedIterator; use crate::net::{Shutdown, SocketAddr, ToSocketAddrs}; +use crate::sync::Arc; use crate::sys_common::{AsInner, FromInner, IntoInner, net as net_imp}; use crate::time::Duration; @@ -723,6 +724,47 @@ impl fmt::Debug for TcpStream { } } +#[stable(feature = "io_traits_arc_more", since = "CURRENT_RUSTC_VERSION")] +impl Read for Arc { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (&**self).read(buf) + } + + fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> { + (&**self).read_buf(buf) + } + + fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result { + (&**self).read_vectored(bufs) + } + + #[inline] + fn is_read_vectored(&self) -> bool { + (&**self).is_read_vectored() + } +} + +#[stable(feature = "io_traits_arc_more", since = "CURRENT_RUSTC_VERSION")] +impl Write for Arc { + fn write(&mut self, buf: &[u8]) -> io::Result { + (&**self).write(buf) + } + + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + (&**self).write_vectored(bufs) + } + + #[inline] + fn is_write_vectored(&self) -> bool { + (&**self).is_write_vectored() + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + (&**self).flush() + } +} + impl TcpListener { /// Creates a new `TcpListener` which will be bound to the specified /// address. From af69df7c82d0fc7f94c73f470c2d38abeddf0499 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 11 Dec 2024 19:38:53 -0500 Subject: [PATCH 2/2] impl `Read` and `Write` on `Arc` By analogy to #94748, this change makes `Arc` implement these traits that are already implemented on `&UnixStream`. --- library/std/src/os/unix/net/stream.rs | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index cb210b41eae19..eb448bf094927 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -18,6 +18,7 @@ use crate::net::Shutdown; use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use crate::path::Path; use crate::sealed::Sealed; +use crate::sync::Arc; use crate::sys::cvt; use crate::sys::net::Socket; use crate::sys_common::{AsInner, FromInner}; @@ -649,6 +650,47 @@ impl<'a> io::Write for &'a UnixStream { } } +#[stable(feature = "io_traits_arc_more", since = "CURRENT_RUSTC_VERSION")] +impl io::Read for Arc { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (&**self).read(buf) + } + + fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> { + (&**self).read_buf(buf) + } + + fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result { + (&**self).read_vectored(bufs) + } + + #[inline] + fn is_read_vectored(&self) -> bool { + (&**self).is_read_vectored() + } +} + +#[stable(feature = "io_traits_arc_more", since = "CURRENT_RUSTC_VERSION")] +impl io::Write for Arc { + fn write(&mut self, buf: &[u8]) -> io::Result { + (&**self).write(buf) + } + + fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result { + (&**self).write_vectored(bufs) + } + + #[inline] + fn is_write_vectored(&self) -> bool { + (&**self).is_write_vectored() + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + (&**self).flush() + } +} + #[stable(feature = "unix_socket", since = "1.10.0")] impl AsRawFd for UnixStream { #[inline]