1use crate::backend::c;
2use crate::fd::BorrowedFd;
3use crate::io;
4
5#[cfg(not(any(apple, target_os = "wasi")))]
14#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
15#[cfg_attr(
16 not(any(target_os = "aix", target_os = "cygwin", target_os = "dragonfly")),
17 repr(i32)
18)]
19#[cfg_attr(any(target_os = "cygwin", target_os = "dragonfly"), repr(u64))]
20#[cfg_attr(target_os = "aix", repr(i64))]
21#[non_exhaustive]
22pub enum ClockId {
23 #[doc(alias = "CLOCK_REALTIME")]
25 Realtime = bitcast!(c::CLOCK_REALTIME),
26
27 #[doc(alias = "CLOCK_MONOTONIC")]
29 Monotonic = bitcast!(c::CLOCK_MONOTONIC),
30
31 #[cfg(any(freebsdlike, target_os = "openbsd"))]
40 #[doc(alias = "CLOCK_UPTIME")]
41 Uptime = c::CLOCK_UPTIME,
42
43 #[cfg(not(any(
45 solarish,
46 target_os = "horizon",
47 target_os = "netbsd",
48 target_os = "redox",
49 target_os = "vita"
50 )))]
51 #[doc(alias = "CLOCK_PROCESS_CPUTIME_ID")]
52 ProcessCPUTime = c::CLOCK_PROCESS_CPUTIME_ID,
53
54 #[cfg(not(any(
56 solarish,
57 target_os = "horizon",
58 target_os = "netbsd",
59 target_os = "redox",
60 target_os = "vita"
61 )))]
62 #[doc(alias = "CLOCK_THREAD_CPUTIME_ID")]
63 ThreadCPUTime = c::CLOCK_THREAD_CPUTIME_ID,
64
65 #[cfg(any(linux_kernel, target_os = "freebsd"))]
67 #[doc(alias = "CLOCK_REALTIME_COARSE")]
68 RealtimeCoarse = c::CLOCK_REALTIME_COARSE,
69
70 #[cfg(any(linux_kernel, target_os = "freebsd"))]
72 #[doc(alias = "CLOCK_MONOTONIC_COARSE")]
73 MonotonicCoarse = c::CLOCK_MONOTONIC_COARSE,
74
75 #[cfg(linux_kernel)]
77 #[doc(alias = "CLOCK_MONOTONIC_RAW")]
78 MonotonicRaw = c::CLOCK_MONOTONIC_RAW,
79
80 #[cfg(linux_kernel)]
82 #[doc(alias = "CLOCK_REALTIME_ALARM")]
83 RealtimeAlarm = bitcast!(c::CLOCK_REALTIME_ALARM),
84
85 #[cfg(all(linux_kernel, feature = "linux_4_11"))]
87 #[doc(alias = "CLOCK_TAI")]
88 Tai = bitcast!(c::CLOCK_TAI),
89
90 #[cfg(any(linux_kernel, target_os = "fuchsia", target_os = "openbsd"))]
92 #[doc(alias = "CLOCK_BOOTTIME")]
93 Boottime = bitcast!(c::CLOCK_BOOTTIME),
94
95 #[cfg(any(linux_kernel, target_os = "fuchsia"))]
97 #[doc(alias = "CLOCK_BOOTTIME_ALARM")]
98 BoottimeAlarm = bitcast!(c::CLOCK_BOOTTIME_ALARM),
99}
100
101#[cfg(not(any(apple, target_os = "wasi")))]
102impl 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
146#[cfg(apple)]
155#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
156#[repr(u32)]
157#[non_exhaustive]
158pub enum ClockId {
159 #[doc(alias = "CLOCK_REALTIME")]
161 Realtime = c::CLOCK_REALTIME,
162
163 #[doc(alias = "CLOCK_MONOTONIC")]
165 Monotonic = c::CLOCK_MONOTONIC,
166
167 #[doc(alias = "CLOCK_PROCESS_CPUTIME_ID")]
169 ProcessCPUTime = c::CLOCK_PROCESS_CPUTIME_ID,
170
171 #[doc(alias = "CLOCK_THREAD_CPUTIME_ID")]
173 ThreadCPUTime = c::CLOCK_THREAD_CPUTIME_ID,
174}
175
176#[cfg(apple)]
177impl 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
191#[cfg(not(target_os = "wasi"))]
199#[derive(Debug, Copy, Clone)]
200#[non_exhaustive]
201pub enum DynamicClockId<'a> {
202 Known(ClockId),
204
205 Dynamic(BorrowedFd<'a>),
207
208 #[cfg(any(linux_kernel, target_os = "fuchsia"))]
210 #[doc(alias = "CLOCK_REALTIME_ALARM")]
211 RealtimeAlarm,
212
213 #[cfg(linux_kernel)]
215 #[doc(alias = "CLOCK_TAI")]
216 Tai,
217
218 #[cfg(any(
220 linux_kernel,
221 target_os = "freebsd",
222 target_os = "fuchsia",
223 target_os = "openbsd"
224 ))]
225 #[doc(alias = "CLOCK_BOOTTIME")]
226 Boottime,
227
228 #[cfg(any(linux_kernel, target_os = "fuchsia"))]
230 #[doc(alias = "CLOCK_BOOTTIME_ALARM")]
231 BoottimeAlarm,
232}