[wasi-common]: yanix now returns io::Error directly (#1242)
* 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
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use crate::{
|
||||
file::{FdFlag, OFlag},
|
||||
Error, Result,
|
||||
from_result, from_success_code,
|
||||
};
|
||||
use std::io::Result;
|
||||
use std::os::unix::prelude::*;
|
||||
|
||||
pub unsafe fn dup_fd(fd: RawFd, close_on_exec: bool) -> Result<RawFd> {
|
||||
@@ -9,7 +10,7 @@ pub unsafe fn dup_fd(fd: RawFd, close_on_exec: bool) -> Result<RawFd> {
|
||||
// the minimum duplicated RawFd number. In our case, I don't
|
||||
// think we have to worry about this that much, so passing in
|
||||
// the RawFd descriptor we want duplicated
|
||||
Error::from_result(if close_on_exec {
|
||||
from_result(if close_on_exec {
|
||||
libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, fd)
|
||||
} else {
|
||||
libc::fcntl(fd, libc::F_DUPFD, fd)
|
||||
@@ -17,17 +18,17 @@ pub unsafe fn dup_fd(fd: RawFd, close_on_exec: bool) -> Result<RawFd> {
|
||||
}
|
||||
|
||||
pub unsafe fn get_fd_flags(fd: RawFd) -> Result<FdFlag> {
|
||||
Error::from_result(libc::fcntl(fd, libc::F_GETFD)).map(FdFlag::from_bits_truncate)
|
||||
from_result(libc::fcntl(fd, libc::F_GETFD)).map(FdFlag::from_bits_truncate)
|
||||
}
|
||||
|
||||
pub unsafe fn set_fd_flags(fd: RawFd, flags: FdFlag) -> Result<()> {
|
||||
Error::from_success_code(libc::fcntl(fd, libc::F_SETFD, flags.bits()))
|
||||
from_success_code(libc::fcntl(fd, libc::F_SETFD, flags.bits()))
|
||||
}
|
||||
|
||||
pub unsafe fn get_status_flags(fd: RawFd) -> Result<OFlag> {
|
||||
Error::from_result(libc::fcntl(fd, libc::F_GETFL)).map(OFlag::from_bits_truncate)
|
||||
from_result(libc::fcntl(fd, libc::F_GETFL)).map(OFlag::from_bits_truncate)
|
||||
}
|
||||
|
||||
pub unsafe fn set_status_flags(fd: RawFd, flags: OFlag) -> Result<()> {
|
||||
Error::from_success_code(libc::fcntl(fd, libc::F_SETFL, flags.bits()))
|
||||
from_success_code(libc::fcntl(fd, libc::F_SETFL, flags.bits()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user