* Yanix now returns io::Error This commit may seem somewhat controversial at first, but hear me out first. Currently, Yanix would return a custom error that's a wrapper around three other error types returned by various entities inside Rust's `libstd`. In particular, Yanix's error type would wrap `io::Error`, `num::TryFromIntError` and `ffi::NulError`. It turns out that there is a natural conversion between the first and the last and provided by the standard library, i.e., `From<ffi::NulError> for io::Error` is provided. So at the surface it may seem that only the first two wrapped error types are worth keeping. Digging a little bit deeper into `libstd`, `num::TryFromIntError` is essentially speaking only a marker that the integral conversion went wrong. The struct implementing this error stores a unit type, and nothing more. It therefore seems like a waste to wrap this particular error when we could unify everything under `io::Error`. And so, whenever we perform an int conversion, I suggest we simply remap the error to `io::Error::from_raw_os_error(libc::EOVERFLOW)` since this carries a comparable amount of information. As a result of completely discarding `yanix::Error` custom error type, we are invariably simplifying `yanix` itself, but also allowing `wasi-common` to simplify in several places as well. * Adapt wasi-common to changes in yanix * Add Cargo.lock * Unwrap try_into's where possible * Remove unnecessary type annotation
50 lines
1.4 KiB
Rust
50 lines
1.4 KiB
Rust
use crate::from_result;
|
|
use bitflags::bitflags;
|
|
use std::{convert::TryInto, io::Result, os::unix::prelude::*};
|
|
|
|
bitflags! {
|
|
pub struct PollFlags: libc::c_short {
|
|
const POLLIN = libc::POLLIN;
|
|
const POLLPRI = libc::POLLPRI;
|
|
const POLLOUT = libc::POLLOUT;
|
|
const POLLRDNORM = libc::POLLRDNORM;
|
|
const POLLWRNORM = libc::POLLWRNORM;
|
|
const POLLRDBAND = libc::POLLRDBAND;
|
|
const POLLWRBAND = libc::POLLWRBAND;
|
|
const POLLERR = libc::POLLERR;
|
|
const POLLHUP = libc::POLLHUP;
|
|
const POLLNVAL = libc::POLLNVAL;
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
|
#[repr(C)]
|
|
pub struct PollFd(libc::pollfd);
|
|
|
|
impl PollFd {
|
|
pub unsafe fn new(fd: RawFd, events: PollFlags) -> Self {
|
|
Self(libc::pollfd {
|
|
fd,
|
|
events: events.bits(),
|
|
revents: PollFlags::empty().bits(),
|
|
})
|
|
}
|
|
|
|
pub fn revents(self) -> Option<PollFlags> {
|
|
PollFlags::from_bits(self.0.revents)
|
|
}
|
|
}
|
|
|
|
pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<usize> {
|
|
let nready = from_result(unsafe {
|
|
libc::poll(
|
|
fds.as_mut_ptr() as *mut libc::pollfd,
|
|
fds.len() as libc::nfds_t,
|
|
timeout,
|
|
)
|
|
})?;
|
|
// When poll doesn't fail, its return value is a non-negative int, which will
|
|
// always be convertable to usize, so we can unwrap() here.
|
|
Ok(nready.try_into().unwrap())
|
|
}
|