Skip to content

Commit 10018d8

Browse files
authored
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.
2 parents 97962d7 + 7112474 commit 10018d8

File tree

8 files changed

+34
-1
lines changed

8 files changed

+34
-1
lines changed

library/std/src/fs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,9 @@ impl Seek for &File {
12291229
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
12301230
self.inner.seek(pos)
12311231
}
1232+
fn stream_position(&mut self) -> io::Result<u64> {
1233+
self.inner.tell()
1234+
}
12321235
}
12331236

12341237
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1275,6 +1278,9 @@ impl Seek for File {
12751278
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
12761279
(&*self).seek(pos)
12771280
}
1281+
fn stream_position(&mut self) -> io::Result<u64> {
1282+
(&*self).stream_position()
1283+
}
12781284
}
12791285

12801286
#[stable(feature = "io_traits_arc", since = "1.73.0")]

library/std/src/sys/pal/hermit/fs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ impl File {
421421
Err(Error::from_raw_os_error(22))
422422
}
423423

424+
pub fn tell(&self) -> io::Result<u64> {
425+
self.seek(SeekFrom::Current(0))
426+
}
427+
424428
pub fn duplicate(&self) -> io::Result<File> {
425429
Err(Error::from_raw_os_error(22))
426430
}

library/std/src/sys/pal/solid/fs.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,11 @@ impl File {
452452
abi::SOLID_FS_Lseek(self.fd.raw(), pos, whence)
453453
})
454454
.map_err(|e| e.as_io_error())?;
455-
456455
// Get the new offset
456+
self.tell()
457+
}
458+
459+
pub fn tell(&self) -> io::Result<u64> {
457460
unsafe {
458461
let mut out_offset = MaybeUninit::uninit();
459462
error::SolidError::err_if_negative(abi::SOLID_FS_Ftell(

library/std/src/sys/pal/uefi/fs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ impl File {
258258
self.0
259259
}
260260

261+
pub fn tell(&self) -> io::Result<u64> {
262+
self.0
263+
}
264+
261265
pub fn duplicate(&self) -> io::Result<File> {
262266
self.0
263267
}

library/std/src/sys/pal/unix/fs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,10 @@ impl File {
14371437
Ok(n as u64)
14381438
}
14391439

1440+
pub fn tell(&self) -> io::Result<u64> {
1441+
self.seek(SeekFrom::Current(0))
1442+
}
1443+
14401444
pub fn duplicate(&self) -> io::Result<File> {
14411445
self.0.duplicate().map(File)
14421446
}

library/std/src/sys/pal/unsupported/fs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ impl File {
258258
self.0
259259
}
260260

261+
pub fn tell(&self) -> io::Result<u64> {
262+
self.0
263+
}
264+
261265
pub fn duplicate(&self) -> io::Result<File> {
262266
self.0
263267
}

library/std/src/sys/pal/wasi/fs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,10 @@ impl File {
517517
self.fd.seek(pos)
518518
}
519519

520+
pub fn tell(&self) -> io::Result<u64> {
521+
self.fd.tell()
522+
}
523+
520524
pub fn duplicate(&self) -> io::Result<File> {
521525
// https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-rationale.md#why-no-dup
522526
unsupported()

library/std/src/sys/pal/windows/fs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ impl File {
631631
Ok(newpos as u64)
632632
}
633633

634+
pub fn tell(&self) -> io::Result<u64> {
635+
self.seek(SeekFrom::Current(0))
636+
}
637+
634638
pub fn duplicate(&self) -> io::Result<File> {
635639
Ok(Self { handle: self.handle.try_clone()? })
636640
}

0 commit comments

Comments
 (0)