Skip to content

Commit

Permalink
chore: Refractor error checking
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Feb 28, 2024
1 parent 354fd52 commit dfc03be
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`.
Expand Down Expand Up @@ -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")))]
Expand Down

0 comments on commit dfc03be

Please sign in to comment.