Skip to content

Commit

Permalink
Forward default NamedTempFile methods (#226)
Browse files Browse the repository at this point in the history
Instead of using the default method implementations, forward them to the
underlying file. This allows the user to, e.g., take advantage of
vectorized IO.
  • Loading branch information
Stebalien authored Apr 3, 2023
1 parent c41ee48 commit 9488362
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,12 +916,58 @@ impl<F: Read> Read for NamedTempFile<F> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.as_file_mut().read(buf).with_err_path(|| self.path())
}

fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
self.as_file_mut()
.read_vectored(bufs)
.with_err_path(|| self.path())
}

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.as_file_mut()
.read_to_end(buf)
.with_err_path(|| self.path())
}

fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
self.as_file_mut()
.read_to_string(buf)
.with_err_path(|| self.path())
}

fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.as_file_mut()
.read_exact(buf)
.with_err_path(|| self.path())
}
}

impl Read for &NamedTempFile<File> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.as_file().read(buf).with_err_path(|| self.path())
}

fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
self.as_file()
.read_vectored(bufs)
.with_err_path(|| self.path())
}

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.as_file()
.read_to_end(buf)
.with_err_path(|| self.path())
}

fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
self.as_file()
.read_to_string(buf)
.with_err_path(|| self.path())
}

fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.as_file().read_exact(buf).with_err_path(|| self.path())
}
}

impl<F: Write> Write for NamedTempFile<F> {
Expand All @@ -932,6 +978,24 @@ impl<F: Write> Write for NamedTempFile<F> {
fn flush(&mut self) -> io::Result<()> {
self.as_file_mut().flush().with_err_path(|| self.path())
}

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

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.as_file_mut()
.write_all(buf)
.with_err_path(|| self.path())
}

fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
self.as_file_mut()
.write_fmt(fmt)
.with_err_path(|| self.path())
}
}

impl Write for &NamedTempFile<File> {
Expand All @@ -942,6 +1006,20 @@ impl Write for &NamedTempFile<File> {
fn flush(&mut self) -> io::Result<()> {
self.as_file().flush().with_err_path(|| self.path())
}

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

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.as_file().write_all(buf).with_err_path(|| self.path())
}

fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
self.as_file().write_fmt(fmt).with_err_path(|| self.path())
}
}

impl<F: Seek> Seek for NamedTempFile<F> {
Expand Down

0 comments on commit 9488362

Please sign in to comment.