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