Skip to content

Commit

Permalink
Add Read, Write and Seek impls for Arc<T> where appropriate
Browse files Browse the repository at this point in the history
If `&T` implements these traits, `Arc<T>` 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 rust-lang#53835.
  • Loading branch information
tbu- committed Mar 8, 2022
1 parent b97dc20 commit 2576c23
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions library/std/src/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -219,6 +220,96 @@ impl<B: BufRead + ?Sized> BufRead for Box<B> {
(**self).read_line(buf)
}
}
#[stable(feature = "io_traits_arc", since = "1.61.0")]
impl<R: ?Sized> Read for Arc<R>
where
for<'a> &'a R: Read,
{
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(&**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<usize> {
(&**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<u8>) -> io::Result<usize> {
(&**self).read_to_end(buf)
}

#[inline]
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
(&**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<W: ?Sized> Write for Arc<W>
where
for<'a> &'a W: Write,
{
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(&**self).write(buf)
}

#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
(&**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<S: ?Sized> Seek for Arc<S>
where
for<'a> &'a S: Seek,
{
#[inline]
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(&**self).seek(pos)
}

#[inline]
fn stream_position(&mut self) -> io::Result<u64> {
(&**self).stream_position()
}
}

// =============================================================================
// In-memory buffer implementations
Expand Down

0 comments on commit 2576c23

Please sign in to comment.