Skip to content

Commit 48c9a1c

Browse files
committed
vexos: use io::const_error! where applicable
1 parent 63b2758 commit 48c9a1c

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

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

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl FileAttr {
6666
if let Ok(size) = u64::try_from(unsafe { vex_sdk::vexFileSize(fd) }) {
6767
Ok(Self::File { size })
6868
} else {
69-
Err(io::Error::new(io::ErrorKind::InvalidData, "Failed to get file size"))
69+
Err(io::const_error!(io::ErrorKind::InvalidData, "Failed to get file size"))
7070
}
7171
}
7272

@@ -213,7 +213,7 @@ impl File {
213213
let file = match opts {
214214
// read + write - unsupported
215215
OpenOptions { read: true, write: true, .. } => {
216-
return Err(io::Error::new(
216+
return Err(io::const_error!(
217217
io::ErrorKind::InvalidInput,
218218
"Opening files with read and write access is unsupported on this target",
219219
));
@@ -240,14 +240,14 @@ impl File {
240240
} => unsafe {
241241
if *create_new {
242242
if vex_sdk::vexFileStatus(path.as_ptr()) != 0 {
243-
return Err(io::Error::new(
243+
return Err(io::const_error!(
244244
io::ErrorKind::AlreadyExists,
245245
"File exists",
246246
));
247247
}
248248
} else if !*create {
249249
if vex_sdk::vexFileStatus(path.as_ptr()) == 0 {
250-
return Err(io::Error::new(
250+
return Err(io::const_error!(
251251
io::ErrorKind::NotFound,
252252
"No such file or directory",
253253
));
@@ -268,14 +268,14 @@ impl File {
268268
} => unsafe {
269269
if *create_new {
270270
if vex_sdk::vexFileStatus(path.as_ptr()) != 0 {
271-
return Err(io::Error::new(
271+
return Err(io::const_error!(
272272
io::ErrorKind::AlreadyExists,
273273
"File exists",
274274
));
275275
}
276276
} else if !*create {
277277
if vex_sdk::vexFileStatus(path.as_ptr()) == 0 {
278-
return Err(io::Error::new(
278+
return Err(io::const_error!(
279279
io::ErrorKind::NotFound,
280280
"No such file or directory",
281281
));
@@ -293,12 +293,12 @@ impl File {
293293
},
294294

295295
_ => {
296-
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid argument"));
296+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Invalid argument"));
297297
}
298298
};
299299

300300
if file.is_null() {
301-
Err(io::Error::new(io::ErrorKind::NotFound, "Could not open file"))
301+
Err(io::const_error!(io::ErrorKind::NotFound, "Could not open file"))
302302
} else {
303303
Ok(Self { fd: FileDesc(file) })
304304
}
@@ -347,7 +347,7 @@ impl File {
347347
let read = unsafe { vex_sdk::vexFileRead(buf_ptr.cast(), 1, len, self.fd.0) };
348348

349349
if read < 0 {
350-
Err(io::Error::new(io::ErrorKind::Other, "Could not read from file"))
350+
Err(io::const_error!(io::ErrorKind::Other, "Could not read from file"))
351351
} else {
352352
Ok(read as usize)
353353
}
@@ -373,7 +373,7 @@ impl File {
373373
unsafe { vex_sdk::vexFileWrite(buf_ptr.cast_mut().cast(), 1, len as _, self.fd.0) };
374374

375375
if written < 0 {
376-
Err(io::Error::new(io::ErrorKind::Other, "Could not write to file"))
376+
Err(io::const_error!(io::ErrorKind::Other, "Could not write to file"))
377377
} else {
378378
Ok(written as usize)
379379
}
@@ -399,7 +399,7 @@ impl File {
399399
let position = unsafe { vex_sdk::vexFileTell(self.fd.0) };
400400

401401
position.try_into().map_err(|_| {
402-
io::Error::new(io::ErrorKind::InvalidData, "Failed to get current location in file")
402+
io::const_error!(io::ErrorKind::InvalidData, "Failed to get current location in file")
403403
})
404404
}
405405

@@ -414,7 +414,7 @@ impl File {
414414

415415
fn try_convert_offset<T: TryInto<u32>>(offset: T) -> io::Result<u32> {
416416
offset.try_into().map_err(|_| {
417-
io::Error::new(
417+
io::const_error!(
418418
io::ErrorKind::InvalidInput,
419419
"Cannot seek to an offset too large to fit in a 32 bit integer",
420420
)
@@ -529,77 +529,79 @@ fn map_fresult(fresult: vex_sdk::FRESULT) -> io::Result<()> {
529529
// VEX uses a derivative of FatFs (Xilinx's xilffs library) for filesystem operations.
530530
match fresult {
531531
vex_sdk::FRESULT::FR_OK => Ok(()),
532-
vex_sdk::FRESULT::FR_DISK_ERR => Err(io::Error::new(
532+
vex_sdk::FRESULT::FR_DISK_ERR => Err(io::const_error!(
533533
io::ErrorKind::Uncategorized,
534534
"internal function reported an unrecoverable hard error",
535535
)),
536-
vex_sdk::FRESULT::FR_INT_ERR => Err(io::Error::new(
536+
vex_sdk::FRESULT::FR_INT_ERR => Err(io::const_error!(
537537
io::ErrorKind::Uncategorized,
538538
"internal error in filesystem runtime",
539539
)),
540-
vex_sdk::FRESULT::FR_NOT_READY => Err(io::Error::new(
540+
vex_sdk::FRESULT::FR_NOT_READY => Err(io::const_error!(
541541
io::ErrorKind::Uncategorized,
542542
"the storage device could not be prepared to work",
543543
)),
544-
vex_sdk::FRESULT::FR_NO_FILE => {
545-
Err(io::Error::new(io::ErrorKind::NotFound, "could not find the file in the directory"))
546-
}
547-
vex_sdk::FRESULT::FR_NO_PATH => Err(io::Error::new(
544+
vex_sdk::FRESULT::FR_NO_FILE => Err(io::const_error!(
545+
io::ErrorKind::NotFound,
546+
"could not find the file in the directory"
547+
)),
548+
vex_sdk::FRESULT::FR_NO_PATH => Err(io::const_error!(
548549
io::ErrorKind::NotFound,
549550
"a directory in the path name could not be found",
550551
)),
551-
vex_sdk::FRESULT::FR_INVALID_NAME => Err(io::Error::new(
552+
vex_sdk::FRESULT::FR_INVALID_NAME => Err(io::const_error!(
552553
io::ErrorKind::InvalidInput,
553554
"the given string is invalid as a path name",
554555
)),
555-
vex_sdk::FRESULT::FR_DENIED => Err(io::Error::new(
556+
vex_sdk::FRESULT::FR_DENIED => Err(io::const_error!(
556557
io::ErrorKind::PermissionDenied,
557558
"the required access for this operation was denied",
558559
)),
559-
vex_sdk::FRESULT::FR_EXIST => Err(io::Error::new(
560+
vex_sdk::FRESULT::FR_EXIST => Err(io::const_error!(
560561
io::ErrorKind::AlreadyExists,
561562
"an object with the same name already exists in the directory",
562563
)),
563-
vex_sdk::FRESULT::FR_INVALID_OBJECT => Err(io::Error::new(
564+
vex_sdk::FRESULT::FR_INVALID_OBJECT => Err(io::const_error!(
564565
io::ErrorKind::Uncategorized,
565566
"invalid or null file/directory object",
566567
)),
567-
vex_sdk::FRESULT::FR_WRITE_PROTECTED => Err(io::Error::new(
568+
vex_sdk::FRESULT::FR_WRITE_PROTECTED => Err(io::const_error!(
568569
io::ErrorKind::PermissionDenied,
569570
"a write operation was performed on write-protected media",
570571
)),
571-
vex_sdk::FRESULT::FR_INVALID_DRIVE => Err(io::Error::new(
572+
vex_sdk::FRESULT::FR_INVALID_DRIVE => Err(io::const_error!(
572573
io::ErrorKind::InvalidInput,
573574
"an invalid drive number was specified in the path name",
574575
)),
575-
vex_sdk::FRESULT::FR_NOT_ENABLED => Err(io::Error::new(
576+
vex_sdk::FRESULT::FR_NOT_ENABLED => Err(io::const_error!(
576577
io::ErrorKind::Uncategorized,
577578
"work area for the logical drive has not been registered",
578579
)),
579-
vex_sdk::FRESULT::FR_NO_FILESYSTEM => Err(io::Error::new(
580+
vex_sdk::FRESULT::FR_NO_FILESYSTEM => Err(io::const_error!(
580581
io::ErrorKind::Uncategorized,
581582
"valid FAT volume could not be found on the drive",
582583
)),
583-
vex_sdk::FRESULT::FR_MKFS_ABORTED => {
584-
Err(io::Error::new(io::ErrorKind::Uncategorized, "failed to create filesystem volume"))
585-
}
586-
vex_sdk::FRESULT::FR_TIMEOUT => Err(io::Error::new(
584+
vex_sdk::FRESULT::FR_MKFS_ABORTED => Err(io::const_error!(
585+
io::ErrorKind::Uncategorized,
586+
"failed to create filesystem volume"
587+
)),
588+
vex_sdk::FRESULT::FR_TIMEOUT => Err(io::const_error!(
587589
io::ErrorKind::TimedOut,
588590
"the function was canceled due to a timeout of thread-safe control",
589591
)),
590-
vex_sdk::FRESULT::FR_LOCKED => Err(io::Error::new(
592+
vex_sdk::FRESULT::FR_LOCKED => Err(io::const_error!(
591593
io::ErrorKind::Uncategorized,
592594
"the operation to the object was rejected by file sharing control",
593595
)),
594596
vex_sdk::FRESULT::FR_NOT_ENOUGH_CORE => {
595-
Err(io::Error::new(io::ErrorKind::OutOfMemory, "not enough memory for the operation"))
597+
Err(io::const_error!(io::ErrorKind::OutOfMemory, "not enough memory for the operation"))
596598
}
597-
vex_sdk::FRESULT::FR_TOO_MANY_OPEN_FILES => Err(io::Error::new(
599+
vex_sdk::FRESULT::FR_TOO_MANY_OPEN_FILES => Err(io::const_error!(
598600
io::ErrorKind::Uncategorized,
599601
"maximum number of open files has been reached",
600602
)),
601603
vex_sdk::FRESULT::FR_INVALID_PARAMETER => {
602-
Err(io::Error::new(io::ErrorKind::InvalidInput, "a given parameter was invalid"))
604+
Err(io::const_error!(io::ErrorKind::InvalidInput, "a given parameter was invalid"))
603605
}
604606
_ => unreachable!(), // C-style enum
605607
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl io::Write for Stdout {
5959
}
6060
.try_into()
6161
.map_err(|_| {
62-
io::Error::new(io::ErrorKind::Uncategorized, "Internal write error occurred.")
62+
io::const_error!(io::ErrorKind::Uncategorized, "Internal write error occurred.")
6363
})?;
6464

6565
written += count;

0 commit comments

Comments
 (0)