Files
wasmtime/crates/wasi-common/yanix/src/clock.rs
Jakub Konka 42fae4e3b8 [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
2020-03-06 14:20:54 -08:00

35 lines
1.1 KiB
Rust

use crate::from_success_code;
use std::io::Result;
use std::mem::MaybeUninit;
#[derive(Debug, Copy, Clone)]
pub enum ClockId {
Realtime,
Monotonic,
ProcessCPUTime,
ThreadCPUTime,
}
impl ClockId {
pub fn as_raw(&self) -> libc::clockid_t {
match self {
Self::Realtime => libc::CLOCK_REALTIME,
Self::Monotonic => libc::CLOCK_MONOTONIC,
Self::ProcessCPUTime => libc::CLOCK_PROCESS_CPUTIME_ID,
Self::ThreadCPUTime => libc::CLOCK_THREAD_CPUTIME_ID,
}
}
}
pub fn clock_getres(clock_id: ClockId) -> Result<libc::timespec> {
let mut timespec = MaybeUninit::<libc::timespec>::uninit();
from_success_code(unsafe { libc::clock_getres(clock_id.as_raw(), timespec.as_mut_ptr()) })?;
Ok(unsafe { timespec.assume_init() })
}
pub fn clock_gettime(clock_id: ClockId) -> Result<libc::timespec> {
let mut timespec = MaybeUninit::<libc::timespec>::uninit();
from_success_code(unsafe { libc::clock_gettime(clock_id.as_raw(), timespec.as_mut_ptr()) })?;
Ok(unsafe { timespec.assume_init() })
}