From 2576c2312f82af4336c0aeebf57cf39fd8dec306 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Tue, 8 Mar 2022 19:26:23 +0100 Subject: [PATCH] Add `Read`, `Write` and `Seek` impls for `Arc` where appropriate If `&T` implements these traits, `Arc` has no reason not to do so either. This is useful for operating system handles like `File` or `TcpStream` which don't need a mutable reference to implement these traits. Fix #53835. --- library/std/src/io/impls.rs | 91 +++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index 64d2457bce159..4e0816f8c14b8 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -8,6 +8,7 @@ use crate::io::{ self, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, Write, }; use crate::mem; +use crate::sync::Arc; // ============================================================================= // Forwarding implementations @@ -219,6 +220,96 @@ impl BufRead for Box { (**self).read_line(buf) } } +#[stable(feature = "io_traits_arc", since = "1.61.0")] +impl Read for Arc +where + for<'a> &'a R: Read, +{ + #[inline] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + (&**self).read(buf) + } + + #[inline] + fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + (&**self).read_buf(buf) + } + + #[inline] + 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() + } + + #[inline] + fn read_to_end(&mut self, buf: &mut Vec) -> io::Result { + (&**self).read_to_end(buf) + } + + #[inline] + fn read_to_string(&mut self, buf: &mut String) -> io::Result { + (&**self).read_to_string(buf) + } + + #[inline] + fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { + (&**self).read_exact(buf) + } +} +#[stable(feature = "io_traits_arc", since = "1.61.0")] +impl Write for Arc +where + for<'a> &'a W: Write, +{ + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { + (&**self).write(buf) + } + + #[inline] + 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() + } + + #[inline] + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + (&**self).write_all(buf) + } + + #[inline] + fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { + (&**self).write_fmt(fmt) + } +} +#[stable(feature = "io_traits_arc", since = "1.61.0")] +impl Seek for Arc +where + for<'a> &'a S: Seek, +{ + #[inline] + fn seek(&mut self, pos: SeekFrom) -> io::Result { + (&**self).seek(pos) + } + + #[inline] + fn stream_position(&mut self) -> io::Result { + (&**self).stream_position() + } +} // ============================================================================= // In-memory buffer implementations