use crate::backend::c;
use crate::fd::BorrowedFd;
use crate::io;
#[cfg(not(any(apple, target_os = "wasi")))]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(
not(any(target_os = "aix", target_os = "cygwin", target_os = "dragonfly")),
repr(i32)
)]
#[cfg_attr(any(target_os = "cygwin", target_os = "dragonfly"), repr(u64))]
#[cfg_attr(target_os = "aix", repr(i64))]
#[non_exhaustive]
pub enum ClockId {
#[doc(alias = "CLOCK_REALTIME")]
Realtime = bitcast!(c::CLOCK_REALTIME),
#[doc(alias = "CLOCK_MONOTONIC")]
Monotonic = bitcast!(c::CLOCK_MONOTONIC),
#[cfg(any(freebsdlike, target_os = "openbsd"))]
#[doc(alias = "CLOCK_UPTIME")]
Uptime = c::CLOCK_UPTIME,
#[cfg(not(any(
solarish,
target_os = "horizon",
target_os = "netbsd",
target_os = "redox",
target_os = "vita"
)))]
#[doc(alias = "CLOCK_PROCESS_CPUTIME_ID")]
ProcessCPUTime = c::CLOCK_PROCESS_CPUTIME_ID,
#[cfg(not(any(
solarish,
target_os = "horizon",
target_os = "netbsd",
target_os = "redox",
target_os = "vita"
)))]
#[doc(alias = "CLOCK_THREAD_CPUTIME_ID")]
ThreadCPUTime = c::CLOCK_THREAD_CPUTIME_ID,
#[cfg(any(linux_kernel, target_os = "freebsd"))]
#[doc(alias = "CLOCK_REALTIME_COARSE")]
RealtimeCoarse = c::CLOCK_REALTIME_COARSE,
#[cfg(any(linux_kernel, target_os = "freebsd"))]
#[doc(alias = "CLOCK_MONOTONIC_COARSE")]
MonotonicCoarse = c::CLOCK_MONOTONIC_COARSE,
#[cfg(linux_kernel)]
#[doc(alias = "CLOCK_MONOTONIC_RAW")]
MonotonicRaw = c::CLOCK_MONOTONIC_RAW,
#[cfg(linux_kernel)]
#[doc(alias = "CLOCK_REALTIME_ALARM")]
RealtimeAlarm = bitcast!(c::CLOCK_REALTIME_ALARM),
#[cfg(all(linux_kernel, feature = "linux_4_11"))]
#[doc(alias = "CLOCK_TAI")]
Tai = bitcast!(c::CLOCK_TAI),
#[cfg(any(linux_kernel, target_os = "fuchsia", target_os = "openbsd"))]
#[doc(alias = "CLOCK_BOOTTIME")]
Boottime = bitcast!(c::CLOCK_BOOTTIME),
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
#[doc(alias = "CLOCK_BOOTTIME_ALARM")]
BoottimeAlarm = bitcast!(c::CLOCK_BOOTTIME_ALARM),
}
#[cfg(not(any(apple, target_os = "wasi")))]
impl TryFrom<c::clockid_t> for ClockId {
type Error = io::Errno;
fn try_from(value: c::clockid_t) -> Result<Self, Self::Error> {
match value {
c::CLOCK_REALTIME => Ok(ClockId::Realtime),
c::CLOCK_MONOTONIC => Ok(ClockId::Monotonic),
#[cfg(any(freebsdlike, target_os = "openbsd"))]
c::CLOCK_UPTIME => Ok(ClockId::Uptime),
#[cfg(not(any(
solarish,
target_os = "horizon",
target_os = "netbsd",
target_os = "redox",
target_os = "vita"
)))]
c::CLOCK_PROCESS_CPUTIME_ID => Ok(ClockId::ProcessCPUTime),
#[cfg(not(any(
solarish,
target_os = "horizon",
target_os = "netbsd",
target_os = "redox",
target_os = "vita"
)))]
c::CLOCK_THREAD_CPUTIME_ID => Ok(ClockId::ThreadCPUTime),
#[cfg(any(linux_kernel, target_os = "freebsd"))]
c::CLOCK_REALTIME_COARSE => Ok(ClockId::RealtimeCoarse),
#[cfg(any(linux_kernel, target_os = "freebsd"))]
c::CLOCK_MONOTONIC_COARSE => Ok(ClockId::MonotonicCoarse),
#[cfg(linux_kernel)]
c::CLOCK_MONOTONIC_RAW => Ok(ClockId::MonotonicRaw),
#[cfg(linux_kernel)]
c::CLOCK_REALTIME_ALARM => Ok(ClockId::RealtimeAlarm),
#[cfg(all(linux_kernel, feature = "linux_4_11"))]
c::CLOCK_TAI => Ok(ClockId::Tai),
#[cfg(any(linux_kernel, target_os = "fuchsia", target_os = "openbsd"))]
c::CLOCK_BOOTTIME => Ok(ClockId::Boottime),
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
c::CLOCK_BOOTTIME_ALARM => Ok(ClockId::BoottimeAlarm),
_ => Err(io::Errno::RANGE),
}
}
}
#[cfg(apple)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(u32)]
#[non_exhaustive]
pub enum ClockId {
#[doc(alias = "CLOCK_REALTIME")]
Realtime = c::CLOCK_REALTIME,
#[doc(alias = "CLOCK_MONOTONIC")]
Monotonic = c::CLOCK_MONOTONIC,
#[doc(alias = "CLOCK_PROCESS_CPUTIME_ID")]
ProcessCPUTime = c::CLOCK_PROCESS_CPUTIME_ID,
#[doc(alias = "CLOCK_THREAD_CPUTIME_ID")]
ThreadCPUTime = c::CLOCK_THREAD_CPUTIME_ID,
}
#[cfg(apple)]
impl TryFrom<c::clockid_t> for ClockId {
type Error = io::Errno;
fn try_from(value: c::clockid_t) -> Result<Self, Self::Error> {
match value {
c::CLOCK_REALTIME => Ok(ClockId::Realtime),
c::CLOCK_MONOTONIC => Ok(ClockId::Monotonic),
c::CLOCK_PROCESS_CPUTIME_ID => Ok(ClockId::ProcessCPUTime),
c::CLOCK_THREAD_CPUTIME_ID => Ok(ClockId::ThreadCPUTime),
_ => Err(io::Errno::RANGE),
}
}
}
#[cfg(not(target_os = "wasi"))]
#[derive(Debug, Copy, Clone)]
#[non_exhaustive]
pub enum DynamicClockId<'a> {
Known(ClockId),
Dynamic(BorrowedFd<'a>),
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
#[doc(alias = "CLOCK_REALTIME_ALARM")]
RealtimeAlarm,
#[cfg(linux_kernel)]
#[doc(alias = "CLOCK_TAI")]
Tai,
#[cfg(any(
linux_kernel,
target_os = "freebsd",
target_os = "fuchsia",
target_os = "openbsd"
))]
#[doc(alias = "CLOCK_BOOTTIME")]
Boottime,
#[cfg(any(linux_kernel, target_os = "fuchsia"))]
#[doc(alias = "CLOCK_BOOTTIME_ALARM")]
BoottimeAlarm,
}