|
1 | 1 | use crate::backend::c;
|
2 | 2 | use crate::fd::BorrowedFd;
|
| 3 | +use crate::io; |
3 | 4 |
|
4 | 5 | /// `CLOCK_*` constants for use with [`clock_gettime`].
|
5 | 6 | ///
|
@@ -97,6 +98,51 @@ pub enum ClockId {
|
97 | 98 | BoottimeAlarm = bitcast!(c::CLOCK_BOOTTIME_ALARM),
|
98 | 99 | }
|
99 | 100 |
|
| 101 | +#[cfg(not(any(apple, target_os = "wasi")))] |
| 102 | +impl TryFrom<c::clockid_t> for ClockId { |
| 103 | + type Error = io::Errno; |
| 104 | + |
| 105 | + fn try_from(value: c::clockid_t) -> Result<Self, Self::Error> { |
| 106 | + match value { |
| 107 | + c::CLOCK_REALTIME => Ok(ClockId::Realtime), |
| 108 | + c::CLOCK_MONOTONIC => Ok(ClockId::Monotonic), |
| 109 | + #[cfg(any(freebsdlike, target_os = "openbsd"))] |
| 110 | + c::CLOCK_UPTIME => Ok(ClockId::Uptime), |
| 111 | + #[cfg(not(any( |
| 112 | + solarish, |
| 113 | + target_os = "horizon", |
| 114 | + target_os = "netbsd", |
| 115 | + target_os = "redox", |
| 116 | + target_os = "vita" |
| 117 | + )))] |
| 118 | + c::CLOCK_PROCESS_CPUTIME_ID => Ok(ClockId::ProcessCPUTime), |
| 119 | + #[cfg(not(any( |
| 120 | + solarish, |
| 121 | + target_os = "horizon", |
| 122 | + target_os = "netbsd", |
| 123 | + target_os = "redox", |
| 124 | + target_os = "vita" |
| 125 | + )))] |
| 126 | + c::CLOCK_THREAD_CPUTIME_ID => Ok(ClockId::ThreadCPUTime), |
| 127 | + #[cfg(any(linux_kernel, target_os = "freebsd"))] |
| 128 | + c::CLOCK_REALTIME_COARSE => Ok(ClockId::RealtimeCoarse), |
| 129 | + #[cfg(any(linux_kernel, target_os = "freebsd"))] |
| 130 | + c::CLOCK_MONOTONIC_COARSE => Ok(ClockId::MonotonicCoarse), |
| 131 | + #[cfg(linux_kernel)] |
| 132 | + c::CLOCK_MONOTONIC_RAW => Ok(ClockId::MonotonicRaw), |
| 133 | + #[cfg(linux_kernel)] |
| 134 | + c::CLOCK_REALTIME_ALARM => Ok(ClockId::RealtimeAlarm), |
| 135 | + #[cfg(all(linux_kernel, feature = "linux_4_11"))] |
| 136 | + c::CLOCK_TAI => Ok(ClockId::Tai), |
| 137 | + #[cfg(any(linux_kernel, target_os = "fuchsia", target_os = "openbsd"))] |
| 138 | + c::CLOCK_BOOTTIME => Ok(ClockId::Boottime), |
| 139 | + #[cfg(any(linux_kernel, target_os = "fuchsia"))] |
| 140 | + c::CLOCK_BOOTTIME_ALARM => Ok(ClockId::BoottimeAlarm), |
| 141 | + _ => Err(io::Errno::RANGE), |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
100 | 146 | /// `CLOCK_*` constants for use with [`clock_gettime`].
|
101 | 147 | ///
|
102 | 148 | /// These constants are always supported at runtime, so `clock_gettime` never
|
@@ -127,6 +173,21 @@ pub enum ClockId {
|
127 | 173 | ThreadCPUTime = c::CLOCK_THREAD_CPUTIME_ID,
|
128 | 174 | }
|
129 | 175 |
|
| 176 | +#[cfg(apple)] |
| 177 | +impl TryFrom<c::clockid_t> for ClockId { |
| 178 | + type Error = io::Errno; |
| 179 | + |
| 180 | + fn try_from(value: c::clockid_t) -> Result<Self, Self::Error> { |
| 181 | + match value { |
| 182 | + c::CLOCK_REALTIME => Ok(ClockId::Realtime), |
| 183 | + c::CLOCK_MONOTONIC => Ok(ClockId::Monotonic), |
| 184 | + c::CLOCK_PROCESS_CPUTIME_ID => Ok(ClockId::ProcessCPUTime), |
| 185 | + c::CLOCK_THREAD_CPUTIME_ID => Ok(ClockId::ThreadCPUTime), |
| 186 | + _ => Err(io::Errno::RANGE), |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
130 | 191 | /// `CLOCK_*` constants for use with [`clock_gettime_dynamic`].
|
131 | 192 | ///
|
132 | 193 | /// These constants may be unsupported at runtime, depending on the OS version,
|
|
0 commit comments