Rollup merge of #155684 - bushrat011899:blanket_io_seek_for_ref, r=jhpratt

Generalize IO Traits for `Arc<T>` where `&T: IoTrait`

ACP: https://github.com/rust-lang/libs-team/issues/755
Tracking issue: https://github.com/rust-lang/rust/issues/154046
Related: rust-lang/rust#94744

## Description

After experimenting with rust-lang/rust#155625, I noticed `Seek` and `SeekFrom` can almost be moved to `core::io`. Unfortunately, the implementation of `Seek` for `Arc<File>` is a blocker for such a move, since `Arc` is not a fundamental type. This PR attempts to resolve this potential blocker by replacing the implementation with a more general alternative. An internal trait `IoHandle` has been added which types can implement to opt-in to `Read`/`Write`/`Seek` implementations for `Arc<Self>` as long as `&Self` implements said trait. Note that `BufRead` is excluded as the signature for `fill_buf` would require returning from a temporary.

Since this "blanket" implementation only applies to a single type which already implements the same traits, I believe this should have no user-facing impact.

If this PR was merged, rust-lang/rust#134190 could be replaced with a 2 line PR:
```rust
impl IoHandle for TcpStream {}
impl IoHandle for UnixStream {}
```
Likewise for any other types, a table of which can be found [here](https://github.com/rust-lang/libs-team/issues/504#issuecomment-2539569736). This is out of scope for this PR to avoid the need for an ACP.

---

## Notes

* See [this comment](https://github.com/rust-lang/rust/issues/154046#issuecomment-4303975612) for further details.
* No AI tooling of any kind was used during the creation of this PR.
This commit is contained in:
Jacob Pratt
2026-04-24 02:42:49 -04:00
committed by GitHub
3 changed files with 136 additions and 53 deletions
+1 -53
View File
@@ -45,7 +45,6 @@
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};
@@ -1541,58 +1540,7 @@ fn stream_position(&mut self) -> io::Result<u64> {
(&*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.
+120
View File
@@ -4,6 +4,7 @@
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};
// =============================================================================
@@ -715,3 +716,122 @@ fn flush(&mut self) -> io::Result<()> {
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
View File
@@ -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 {