Skip to content

Commit a4701ce

Browse files
committed
fix and clean up comments surrounding file seeking
1 parent ed57739 commit a4701ce

File tree

1 file changed

+6
-21
lines changed
  • library/std/src/sys/pal/vexos

1 file changed

+6
-21
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -339,33 +339,19 @@ impl File {
339339
SeekFrom::Start(offset) => unsafe {
340340
map_fresult(vex_sdk::vexFileSeek(self.fd.0, try_convert_offset(offset)?, SEEK_SET))?
341341
},
342-
343-
// VEXos does not allow seeking with negative offsets.
344-
// That means we need to calculate the offset from the start for both of these.
345342
SeekFrom::End(offset) => unsafe {
346-
// If our offset is positive, everything is easy
347343
if offset >= 0 {
348344
map_fresult(vex_sdk::vexFileSeek(
349345
self.fd.0,
350346
try_convert_offset(offset)?,
351347
SEEK_END,
352348
))?
353349
} else {
354-
// Get the position of the end of the file...
350+
// `vexFileSeek` does not support seeking with negative offset, meaning
351+
// we have to calculate the offset from the end of the file ourselves.
355352
map_fresult(vex_sdk::vexFileSeek(
356353
self.fd.0,
357-
try_convert_offset(offset)?,
358-
SEEK_END,
359-
))?;
360-
// The number returned by the VEX SDK tell is stored as a 32 bit interger,
361-
// and therefore this conversion cannot fail.
362-
let position = self.tell()? as i64;
363-
364-
// Offset from that position
365-
let new_position = position + offset;
366-
map_fresult(vex_sdk::vexFileSeek(
367-
self.fd.0,
368-
try_convert_offset(new_position)?,
354+
try_convert_offset(self.file_attr().size + offset)?,
369355
SEEK_SET,
370356
))?
371357
}
@@ -378,12 +364,11 @@ impl File {
378364
SEEK_CUR,
379365
))?
380366
} else {
381-
let position = self.tell()? as i64;
382-
383-
let new_position = position + offset;
367+
// `vexFileSeek` does not support seeking with negative offset, meaning
368+
// we have to calculate the offset from the stream position ourselves.
384369
map_fresult(vex_sdk::vexFileSeek(
385370
self.fd.0,
386-
try_convert_offset(new_position)?,
371+
try_convert_offset((self.tell()? as i64) + offset)?,
387372
SEEK_SET,
388373
))?
389374
}

0 commit comments

Comments
 (0)