Skip to content

Commit 2576c23

Browse files
committed
Add Read, Write and Seek impls for Arc<T> where appropriate
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.
1 parent b97dc20 commit 2576c23

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

library/std/src/io/impls.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::io::{
88
self, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, Write,
99
};
1010
use crate::mem;
11+
use crate::sync::Arc;
1112

1213
// =============================================================================
1314
// Forwarding implementations
@@ -219,6 +220,96 @@ impl<B: BufRead + ?Sized> BufRead for Box<B> {
219220
(**self).read_line(buf)
220221
}
221222
}
223+
#[stable(feature = "io_traits_arc", since = "1.61.0")]
224+
impl<R: ?Sized> Read for Arc<R>
225+
where
226+
for<'a> &'a R: Read,
227+
{
228+
#[inline]
229+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
230+
(&**self).read(buf)
231+
}
232+
233+
#[inline]
234+
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
235+
(&**self).read_buf(buf)
236+
}
237+
238+
#[inline]
239+
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
240+
(&**self).read_vectored(bufs)
241+
}
242+
243+
#[inline]
244+
fn is_read_vectored(&self) -> bool {
245+
(&**self).is_read_vectored()
246+
}
247+
248+
#[inline]
249+
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
250+
(&**self).read_to_end(buf)
251+
}
252+
253+
#[inline]
254+
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
255+
(&**self).read_to_string(buf)
256+
}
257+
258+
#[inline]
259+
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
260+
(&**self).read_exact(buf)
261+
}
262+
}
263+
#[stable(feature = "io_traits_arc", since = "1.61.0")]
264+
impl<W: ?Sized> Write for Arc<W>
265+
where
266+
for<'a> &'a W: Write,
267+
{
268+
#[inline]
269+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
270+
(&**self).write(buf)
271+
}
272+
273+
#[inline]
274+
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
275+
(&**self).write_vectored(bufs)
276+
}
277+
278+
#[inline]
279+
fn is_write_vectored(&self) -> bool {
280+
(&**self).is_write_vectored()
281+
}
282+
283+
#[inline]
284+
fn flush(&mut self) -> io::Result<()> {
285+
(&**self).flush()
286+
}
287+
288+
#[inline]
289+
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
290+
(&**self).write_all(buf)
291+
}
292+
293+
#[inline]
294+
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
295+
(&**self).write_fmt(fmt)
296+
}
297+
}
298+
#[stable(feature = "io_traits_arc", since = "1.61.0")]
299+
impl<S: ?Sized> Seek for Arc<S>
300+
where
301+
for<'a> &'a S: Seek,
302+
{
303+
#[inline]
304+
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
305+
(&**self).seek(pos)
306+
}
307+
308+
#[inline]
309+
fn stream_position(&mut self) -> io::Result<u64> {
310+
(&**self).stream_position()
311+
}
312+
}
222313

223314
// =============================================================================
224315
// In-memory buffer implementations

0 commit comments

Comments
 (0)