Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 1 addition & 53 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ use crate::ffi::OsString;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
use crate::path::{Path, PathBuf};
use crate::sealed::Sealed;
use crate::sync::Arc;
use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner, fs as fs_imp};
use crate::time::SystemTime;
use crate::{error, fmt};
Expand Down Expand Up @@ -1541,58 +1540,7 @@ impl Seek for File {
(&*self).stream_position()
}
}

#[stable(feature = "io_traits_arc", since = "1.73.0")]
impl Read for Arc<File> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(&**self).read(buf)
}
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
(&**self).read_vectored(bufs)
}
fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
(&**self).read_buf(cursor)
}
#[inline]
fn is_read_vectored(&self) -> bool {
(&**self).is_read_vectored()
}
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
(&**self).read_to_end(buf)
}
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
(&**self).read_to_string(buf)
}
}
#[stable(feature = "io_traits_arc", since = "1.73.0")]
impl Write for Arc<File> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(&**self).write(buf)
}
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()
}
}
#[stable(feature = "io_traits_arc", since = "1.73.0")]
impl Seek for Arc<File> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(&**self).seek(pos)
}
fn stream_len(&mut self) -> io::Result<u64> {
(&**self).stream_len()
}
fn stream_position(&mut self) -> io::Result<u64> {
(&**self).stream_position()
}
}
impl crate::io::IoHandle for File {}

impl Dir {
/// Attempts to open a directory at `path` in read-only mode.
Expand Down
120 changes: 120 additions & 0 deletions library/std/src/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod tests;
use crate::alloc::Allocator;
use crate::collections::VecDeque;
use crate::io::{self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
use crate::sync::Arc;
use crate::{cmp, fmt, mem, str};

// =============================================================================
Expand Down Expand Up @@ -715,3 +716,122 @@ impl<'a> io::Write for core::io::BorrowedCursor<'a> {
Ok(())
}
}

#[stable(feature = "io_traits_arc", since = "1.73.0")]
impl<R: Read + ?Sized> Read for Arc<R>
where
for<'a> &'a R: Read,
R: crate::io::IoHandle,
{
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(&**self).read(buf)
}

#[inline]
fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
(&**self).read_buf(cursor)
}

#[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)
}

#[inline]
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
(&**self).read_buf_exact(cursor)
}
}
#[stable(feature = "io_traits_arc", since = "1.73.0")]
impl<W: Write + ?Sized> Write for Arc<W>
where
for<'a> &'a W: Write,
W: crate::io::IoHandle,
{
#[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_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
(&**self).write_all_vectored(bufs)
}

#[inline]
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
(&**self).write_fmt(fmt)
}
}
#[stable(feature = "io_traits_arc", since = "1.73.0")]
impl<S: Seek + ?Sized> Seek for Arc<S>
where
for<'a> &'a S: Seek,
S: crate::io::IoHandle,
{
#[inline]
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(&**self).seek(pos)
}

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

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

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

#[inline]
fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
(&**self).seek_relative(offset)
}
}
15 changes: 15 additions & 0 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,21 @@ pub enum SeekFrom {
Current(#[stable(feature = "rust1", since = "1.0.0")] i64),
}

/// Marks that a type `T` can have IO traits such as [`Seek`], [`Write`], etc. automatically
/// implemented for handle types like [`Arc`][arc] as well.
///
/// This trait should only be implemented for types where `<&T as Trait>::method(&mut &value, ..)`
/// would be identical to `<T as Trait>::method(&mut value, ..)`.
///
/// [`File`][file] passes this test, as operations on `&File` and `File` both affect
/// the same underlying file.
/// `[u8]` fails, because any modification to `&mut &[u8]` would only affect a temporary
/// and be lost after the method has been called.
///
/// [file]: crate::fs::File
/// [arc]: crate::sync::Arc
pub(crate) trait IoHandle {}

fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>) -> Result<usize> {
let mut read = 0;
loop {
Expand Down
Loading