chore: Refractor error checking

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley 2024-02-27 21:48:30 -08:00
parent d33b6ce27a
commit 6865887c1a
No known key found for this signature in database
GPG Key ID: 2FE69973CFD64832
1 changed files with 13 additions and 15 deletions

View File

@ -485,22 +485,14 @@ mod syscall {
pub(super) fn read(fd: BorrowedFd<'_>, bytes: &mut [u8]) -> io::Result<usize> {
let count = unsafe { hermit_abi::read(fd.as_raw_fd(), bytes.as_mut_ptr(), bytes.len()) };
if count == -1 {
Err(io::Error::last_os_error())
} else {
Ok(count as usize)
}
cvt(count)
}
/// Write some bytes.
pub(super) fn write(fd: BorrowedFd<'_>, bytes: &[u8]) -> io::Result<usize> {
let count = unsafe { hermit_abi::write(fd.as_raw_fd(), bytes.as_ptr(), bytes.len()) };
if count == -1 {
Err(io::Error::last_os_error())
} else {
Ok(count as usize)
}
cvt(count)
}
/// Safe wrapper around the `poll` system call.
@ -513,11 +505,7 @@ mod syscall {
)
};
if call == -1 {
Err(io::Error::last_os_error())
} else {
Ok(call as usize)
}
cvt(call as isize)
}
/// Safe wrapper around `pollfd`.
@ -618,6 +606,16 @@ mod syscall {
Self
}
}
/// Convert a number to an actual result.
#[inline]
fn cvt(len: isize) -> io::Result<usize> {
if len == -1 {
Err(io::Error::last_os_error())
} else {
Ok(len as usize)
}
}
}
#[cfg(not(any(target_os = "espidf", target_os = "hermit")))]