Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Read, Write and Seek impls for Arc<File> where appropriate #94748

Merged
merged 2 commits into from
Jul 10, 2023
Merged
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
97 changes: 65 additions & 32 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::fmt;
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::fs as fs_imp;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
use crate::time::SystemTime;
Expand Down Expand Up @@ -743,7 +744,7 @@ fn buffer_capacity_required(mut file: &File) -> Option<usize> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Read for File {
impl Read for &File {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
Expand Down Expand Up @@ -776,7 +777,7 @@ impl Read for File {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Write for File {
impl Write for &File {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
Expand All @@ -795,67 +796,99 @@ impl Write for File {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Seek for File {
impl Seek for &File {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.inner.seek(pos)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Read for &File {
impl Read for File {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change correct wrt the resolver?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read (etc.) are still implemented for File and &File. No changes made to the outside. I just moved the implementation into the &File impl because I can forward the File impl to the &File one.

fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
(&*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.inner.read_buf(cursor)
(&*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 = "rust1", since = "1.0.0")]
impl Write for 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()
}
fn flush(&mut self) -> io::Result<()> {
(&*self).flush()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Seek for File {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(&*self).seek(pos)
}
}

#[stable(feature = "io_traits_arc", since = "CURRENT_RUSTC_VERSION")]
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.inner.read_vectored(bufs)
(&**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.inner.is_read_vectored()
(&**self).is_read_vectored()
}

// Reserves space in the buffer based on the file size when available.
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
let size = buffer_capacity_required(self);
buf.reserve(size.unwrap_or(0));
io::default_read_to_end(self, buf, size)
(&**self).read_to_end(buf)
}

// Reserves space in the buffer based on the file size when available.
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
let size = buffer_capacity_required(self);
buf.reserve(size.unwrap_or(0));
io::default_read_to_string(self, buf, size)
(&**self).read_to_string(buf)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Write for &File {
#[stable(feature = "io_traits_arc", since = "CURRENT_RUSTC_VERSION")]
impl Write for Arc<File> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
(&**self).write(buf)
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.inner.write_vectored(bufs)
(&**self).write_vectored(bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
self.inner.is_write_vectored()
(&**self).is_write_vectored()
}

fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
(&**self).flush()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Seek for &File {
#[stable(feature = "io_traits_arc", since = "CURRENT_RUSTC_VERSION")]
impl Seek for Arc<File> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.inner.seek(pos)
(&**self).seek(pos)
}
}

Expand Down
Loading