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:
commit
10018d8e10
8 changed files with 34 additions and 1 deletions
|
@ -1229,6 +1229,9 @@ impl Seek for &File {
|
|||
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
||||
self.inner.seek(pos)
|
||||
}
|
||||
fn stream_position(&mut self) -> io::Result<u64> {
|
||||
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<u64> {
|
||||
(&*self).seek(pos)
|
||||
}
|
||||
fn stream_position(&mut self) -> io::Result<u64> {
|
||||
(&*self).stream_position()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "io_traits_arc", since = "1.73.0")]
|
||||
|
|
|
@ -421,6 +421,10 @@ impl File {
|
|||
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> {
|
||||
Err(Error::from_raw_os_error(22))
|
||||
}
|
||||
|
|
|
@ -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<u64> {
|
||||
unsafe {
|
||||
let mut out_offset = MaybeUninit::uninit();
|
||||
error::SolidError::err_if_negative(abi::SOLID_FS_Ftell(
|
||||
|
|
|
@ -258,6 +258,10 @@ impl File {
|
|||
self.0
|
||||
}
|
||||
|
||||
pub fn tell(&self) -> io::Result<u64> {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> io::Result<File> {
|
||||
self.0
|
||||
}
|
||||
|
|
|
@ -1437,6 +1437,10 @@ impl File {
|
|||
Ok(n as u64)
|
||||
}
|
||||
|
||||
pub fn tell(&self) -> io::Result<u64> {
|
||||
self.seek(SeekFrom::Current(0))
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> io::Result<File> {
|
||||
self.0.duplicate().map(File)
|
||||
}
|
||||
|
|
|
@ -258,6 +258,10 @@ impl File {
|
|||
self.0
|
||||
}
|
||||
|
||||
pub fn tell(&self) -> io::Result<u64> {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> io::Result<File> {
|
||||
self.0
|
||||
}
|
||||
|
|
|
@ -517,6 +517,10 @@ impl File {
|
|||
self.fd.seek(pos)
|
||||
}
|
||||
|
||||
pub fn tell(&self) -> io::Result<u64> {
|
||||
self.fd.tell()
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> io::Result<File> {
|
||||
// https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-rationale.md#why-no-dup
|
||||
unsupported()
|
||||
|
|
|
@ -631,6 +631,10 @@ impl File {
|
|||
Ok(newpos as u64)
|
||||
}
|
||||
|
||||
pub fn tell(&self) -> io::Result<u64> {
|
||||
self.seek(SeekFrom::Current(0))
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> io::Result<File> {
|
||||
Ok(Self { handle: self.handle.try_clone()? })
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue