1
Fork 0

Rollup merge of #137165 - thaliaarchi:file-tell, r=ChrisDenton

Use `tell` for `<File as Seek>::stream_position`

Some platforms have a more efficient way to get the current offset of the file than by seeking. For example, Wasi has `fd_tell` and SOLID has `SOLID_FS_Ftell`. Implement `<File as Seek>::stream_position()` in terms of those.

I do not use any APIs that were not already used in `std`. Although, the `libc` crate has [`ftell`](https://docs.rs/libc/latest/libc/fn.ftell.html), [`ftello`](https://docs.rs/libc/latest/libc/fn.ftello.html), and [`ftello64`](https://docs.rs/libc/latest/libc/fn.ftello64.html), I do not know platform coverage. It appears that Windows has no `tell`-like API.

I have checked that it builds on each relevant platform.
This commit is contained in:
Matthias Krüger 2025-02-17 17:06:10 +01:00 committed by GitHub
commit 10018d8e10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 34 additions and 1 deletions

View file

@ -1229,6 +1229,9 @@ impl Seek for &File {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.inner.seek(pos) self.inner.seek(pos)
} }
fn stream_position(&mut self) -> io::Result<u64> {
self.inner.tell()
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
@ -1275,6 +1278,9 @@ impl Seek for File {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
(&*self).seek(pos) (&*self).seek(pos)
} }
fn stream_position(&mut self) -> io::Result<u64> {
(&*self).stream_position()
}
} }
#[stable(feature = "io_traits_arc", since = "1.73.0")] #[stable(feature = "io_traits_arc", since = "1.73.0")]

View file

@ -421,6 +421,10 @@ impl File {
Err(Error::from_raw_os_error(22)) Err(Error::from_raw_os_error(22))
} }
pub fn tell(&self) -> io::Result<u64> {
self.seek(SeekFrom::Current(0))
}
pub fn duplicate(&self) -> io::Result<File> { pub fn duplicate(&self) -> io::Result<File> {
Err(Error::from_raw_os_error(22)) Err(Error::from_raw_os_error(22))
} }

View file

@ -452,8 +452,11 @@ impl File {
abi::SOLID_FS_Lseek(self.fd.raw(), pos, whence) abi::SOLID_FS_Lseek(self.fd.raw(), pos, whence)
}) })
.map_err(|e| e.as_io_error())?; .map_err(|e| e.as_io_error())?;
// Get the new offset // Get the new offset
self.tell()
}
pub fn tell(&self) -> io::Result<u64> {
unsafe { unsafe {
let mut out_offset = MaybeUninit::uninit(); let mut out_offset = MaybeUninit::uninit();
error::SolidError::err_if_negative(abi::SOLID_FS_Ftell( error::SolidError::err_if_negative(abi::SOLID_FS_Ftell(

View file

@ -258,6 +258,10 @@ impl File {
self.0 self.0
} }
pub fn tell(&self) -> io::Result<u64> {
self.0
}
pub fn duplicate(&self) -> io::Result<File> { pub fn duplicate(&self) -> io::Result<File> {
self.0 self.0
} }

View file

@ -1437,6 +1437,10 @@ impl File {
Ok(n as u64) Ok(n as u64)
} }
pub fn tell(&self) -> io::Result<u64> {
self.seek(SeekFrom::Current(0))
}
pub fn duplicate(&self) -> io::Result<File> { pub fn duplicate(&self) -> io::Result<File> {
self.0.duplicate().map(File) self.0.duplicate().map(File)
} }

View file

@ -258,6 +258,10 @@ impl File {
self.0 self.0
} }
pub fn tell(&self) -> io::Result<u64> {
self.0
}
pub fn duplicate(&self) -> io::Result<File> { pub fn duplicate(&self) -> io::Result<File> {
self.0 self.0
} }

View file

@ -517,6 +517,10 @@ impl File {
self.fd.seek(pos) self.fd.seek(pos)
} }
pub fn tell(&self) -> io::Result<u64> {
self.fd.tell()
}
pub fn duplicate(&self) -> io::Result<File> { pub fn duplicate(&self) -> io::Result<File> {
// https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-rationale.md#why-no-dup // https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-rationale.md#why-no-dup
unsupported() unsupported()

View file

@ -631,6 +631,10 @@ impl File {
Ok(newpos as u64) Ok(newpos as u64)
} }
pub fn tell(&self) -> io::Result<u64> {
self.seek(SeekFrom::Current(0))
}
pub fn duplicate(&self) -> io::Result<File> { pub fn duplicate(&self) -> io::Result<File> {
Ok(Self { handle: self.handle.try_clone()? }) Ok(Self { handle: self.handle.try_clone()? })
} }