Files
wasmtime/crates/wasi-common/yanix/src/clock.rs
Jakub Konka 135a48ca7e wasi-common error cleanup: part 1, yanix (#1226)
* Reuse std::io::Error for raw *nix errno

This commit removes custom `yanix::Errno` and instead (as was
previously suggested) reuses `std::io::Error` to generate and wrap
raw *nix errno value.

* Update wasi-common to use new Yanix error type

This commit updates `wasi-common` to use new way of handling raw
OS error in `yanix`; i.e., via re-use of `std::io::Error` instead
of a custom `Errno` enum.

* Fix formatting

* Unwrap if io::Error created from raw OS error

This commit calls `unwrap` on `err` if that one was created via
`io::Error::last_os_error()`. It also refactors error matching
in several syscalls on the BSD platform (mainly).
2020-03-05 10:08:28 +01:00

38 lines
1.1 KiB
Rust

use crate::{Error, 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();
Error::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();
Error::from_success_code(unsafe {
libc::clock_gettime(clock_id.as_raw(), timespec.as_mut_ptr())
})?;
Ok(unsafe { timespec.assume_init() })
}