about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTobias Bucher <tobiasbucher5991@gmail.com>2022-12-31 01:43:54 +0100
committerTobias Bucher <tobiasbucher5991@gmail.com>2023-06-23 14:55:43 +0200
commit11fecf619aabd1f3d9a015b925bf0deedc7092e9 (patch)
tree09037fc6eea140a1d4e94ce060a6571cdec8d6f2
parent9fc2da1b5438728e9f6469db227a7de2b345203e (diff)
downloadrust-11fecf619aabd1f3d9a015b925bf0deedc7092e9.tar.gz
rust-11fecf619aabd1f3d9a015b925bf0deedc7092e9.zip
Add `Read`, `Write` and `Seek` impls for `Arc<File>` 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.

CC #53835.
CC #94744.
-rw-r--r--library/std/src/fs.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs
index f5848bf4cc4..901426c5a1c 100644
--- a/library/std/src/fs.rs
+++ b/library/std/src/fs.rs
@@ -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;
@@ -846,6 +847,51 @@ impl Seek for File {
     }
 }
 
+#[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).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 = "CURRENT_RUSTC_VERSION")]
+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()
+    }
+    fn flush(&mut self) -> io::Result<()> {
+        (&**self).flush()
+    }
+}
+#[stable(feature = "io_traits_arc", since = "CURRENT_RUSTC_VERSION")]
+impl Seek for Arc<File> {
+    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+        (&**self).seek(pos)
+    }
+}
+
 impl OpenOptions {
     /// Creates a blank new set of options ready for configuration.
     ///