Skip to content

Commit b599f6c

Browse files
committed
vexos: add SAFETY comments for FileDesc invariants
1 parent d0c43f4 commit b599f6c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ impl File {
340340
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
341341
let len = buf.len() as u32;
342342
let buf_ptr = buf.as_mut_ptr();
343-
let read = unsafe { vex_sdk::vexFileRead(buf_ptr.cast::<c_char>(), 1, len, self.fd.0) };
343+
let read = unsafe {
344+
// SAFETY: `self.fd` contains a valid pointer to `FIL` for this struct's lifetime.
345+
vex_sdk::vexFileRead(buf_ptr.cast::<c_char>(), 1, len, self.fd.0)
346+
};
344347

345348
if read < 0 {
346349
Err(io::const_error!(io::ErrorKind::Other, "Could not read from file"))
@@ -366,6 +369,7 @@ impl File {
366369
let len = buf.len() as u32;
367370
let buf_ptr = buf.as_ptr();
368371
let written = unsafe {
372+
// SAFETY: `self.fd` contains a valid pointer to `FIL` for this struct's lifetime.
369373
vex_sdk::vexFileWrite(buf_ptr.cast_mut().cast::<c_char>(), 1, len, self.fd.0)
370374
};
371375

@@ -387,12 +391,14 @@ impl File {
387391

388392
pub fn flush(&self) -> io::Result<()> {
389393
unsafe {
394+
// SAFETY: `self.fd` contains a valid pointer to `FIL` for this struct's lifetime.
390395
vex_sdk::vexFileSync(self.fd.0);
391396
}
392397
Ok(())
393398
}
394399

395400
pub fn tell(&self) -> io::Result<u64> {
401+
// SAFETY: `self.fd` contains a valid pointer to `FIL` for this struct's lifetime.
396402
let position = unsafe { vex_sdk::vexFileTell(self.fd.0) };
397403

398404
position.try_into().map_err(|_| {
@@ -418,6 +424,7 @@ impl File {
418424
})
419425
}
420426

427+
// SAFETY: `self.fd` contains a valid pointer to `FIL` for this struct's lifetime.
421428
match pos {
422429
SeekFrom::Start(offset) => unsafe {
423430
map_fresult(vex_sdk::vexFileSeek(self.fd.0, try_convert_offset(offset)?, SEEK_SET))?

0 commit comments

Comments
 (0)