From 711247413465fc015420574d6afe929186ab0bb4 Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Sun, 16 Feb 2025 22:11:46 -0800 Subject: [PATCH] Use tell for ::stream_position --- library/std/src/fs.rs | 6 ++++++ library/std/src/sys/pal/hermit/fs.rs | 4 ++++ library/std/src/sys/pal/solid/fs.rs | 5 ++++- library/std/src/sys/pal/uefi/fs.rs | 4 ++++ library/std/src/sys/pal/unix/fs.rs | 4 ++++ library/std/src/sys/pal/unsupported/fs.rs | 4 ++++ library/std/src/sys/pal/wasi/fs.rs | 4 ++++ library/std/src/sys/pal/windows/fs.rs | 4 ++++ 8 files changed, 34 insertions(+), 1 deletion(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 83b009c86dc08..6001a2e2f391f 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1229,6 +1229,9 @@ impl Seek for &File { fn seek(&mut self, pos: SeekFrom) -> io::Result { self.inner.seek(pos) } + fn stream_position(&mut self) -> io::Result { + self.inner.tell() + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1275,6 +1278,9 @@ impl Seek for File { fn seek(&mut self, pos: SeekFrom) -> io::Result { (&*self).seek(pos) } + fn stream_position(&mut self) -> io::Result { + (&*self).stream_position() + } } #[stable(feature = "io_traits_arc", since = "1.73.0")] diff --git a/library/std/src/sys/pal/hermit/fs.rs b/library/std/src/sys/pal/hermit/fs.rs index 783623552bb17..45fe7e093cdcb 100644 --- a/library/std/src/sys/pal/hermit/fs.rs +++ b/library/std/src/sys/pal/hermit/fs.rs @@ -425,6 +425,10 @@ impl File { Err(Error::from_raw_os_error(22)) } + pub fn tell(&self) -> io::Result { + self.seek(SeekFrom::Current(0)) + } + pub fn duplicate(&self) -> io::Result { Err(Error::from_raw_os_error(22)) } diff --git a/library/std/src/sys/pal/solid/fs.rs b/library/std/src/sys/pal/solid/fs.rs index cc424141ea80c..70b5400bde47a 100644 --- a/library/std/src/sys/pal/solid/fs.rs +++ b/library/std/src/sys/pal/solid/fs.rs @@ -452,8 +452,11 @@ impl File { abi::SOLID_FS_Lseek(self.fd.raw(), pos, whence) }) .map_err(|e| e.as_io_error())?; - // Get the new offset + self.tell() + } + + pub fn tell(&self) -> io::Result { unsafe { let mut out_offset = MaybeUninit::uninit(); error::SolidError::err_if_negative(abi::SOLID_FS_Ftell( diff --git a/library/std/src/sys/pal/uefi/fs.rs b/library/std/src/sys/pal/uefi/fs.rs index 9585ec24f687d..45e93deffa3a4 100644 --- a/library/std/src/sys/pal/uefi/fs.rs +++ b/library/std/src/sys/pal/uefi/fs.rs @@ -258,6 +258,10 @@ impl File { self.0 } + pub fn tell(&self) -> io::Result { + self.0 + } + pub fn duplicate(&self) -> io::Result { self.0 } diff --git a/library/std/src/sys/pal/unix/fs.rs b/library/std/src/sys/pal/unix/fs.rs index 00cfa7a7fcfda..e13ce580d451d 100644 --- a/library/std/src/sys/pal/unix/fs.rs +++ b/library/std/src/sys/pal/unix/fs.rs @@ -1438,6 +1438,10 @@ impl File { Ok(n as u64) } + pub fn tell(&self) -> io::Result { + self.seek(SeekFrom::Current(0)) + } + pub fn duplicate(&self) -> io::Result { self.0.duplicate().map(File) } diff --git a/library/std/src/sys/pal/unsupported/fs.rs b/library/std/src/sys/pal/unsupported/fs.rs index 9585ec24f687d..45e93deffa3a4 100644 --- a/library/std/src/sys/pal/unsupported/fs.rs +++ b/library/std/src/sys/pal/unsupported/fs.rs @@ -258,6 +258,10 @@ impl File { self.0 } + pub fn tell(&self) -> io::Result { + self.0 + } + pub fn duplicate(&self) -> io::Result { self.0 } diff --git a/library/std/src/sys/pal/wasi/fs.rs b/library/std/src/sys/pal/wasi/fs.rs index faf3edd5da6ce..513f038b5545b 100644 --- a/library/std/src/sys/pal/wasi/fs.rs +++ b/library/std/src/sys/pal/wasi/fs.rs @@ -517,6 +517,10 @@ impl File { self.fd.seek(pos) } + pub fn tell(&self) -> io::Result { + self.fd.tell() + } + pub fn duplicate(&self) -> io::Result { // https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-rationale.md#why-no-dup unsupported() diff --git a/library/std/src/sys/pal/windows/fs.rs b/library/std/src/sys/pal/windows/fs.rs index 62d4d727432c3..9b283a7a117b2 100644 --- a/library/std/src/sys/pal/windows/fs.rs +++ b/library/std/src/sys/pal/windows/fs.rs @@ -631,6 +631,10 @@ impl File { Ok(newpos as u64) } + pub fn tell(&self) -> io::Result { + self.seek(SeekFrom::Current(0)) + } + pub fn duplicate(&self) -> io::Result { Ok(Self { handle: self.handle.try_clone()? }) }