|
10 | 10 |
|
11 | 11 | pub mod hrtimer;
|
12 | 12 |
|
| 13 | +/// The number of nanoseconds per microsecond. |
| 14 | +pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; |
| 15 | + |
13 | 16 | /// The number of nanoseconds per millisecond.
|
14 | 17 | pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
|
15 | 18 |
|
| 19 | +/// The number of nanoseconds per second. |
| 20 | +pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64; |
| 21 | + |
16 | 22 | /// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
|
17 | 23 | pub type Jiffies = crate::ffi::c_ulong;
|
18 | 24 |
|
@@ -149,3 +155,85 @@ impl ClockId {
|
149 | 155 | self as bindings::clockid_t
|
150 | 156 | }
|
151 | 157 | }
|
| 158 | + |
| 159 | +/// A span of time. |
| 160 | +/// |
| 161 | +/// This struct represents a span of time, with its value stored as nanoseconds. |
| 162 | +/// The value can represent any valid i64 value, including negative, zero, and |
| 163 | +/// positive numbers. |
| 164 | +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)] |
| 165 | +pub struct Delta { |
| 166 | + nanos: i64, |
| 167 | +} |
| 168 | + |
| 169 | +impl Delta { |
| 170 | + /// A span of time equal to zero. |
| 171 | + pub const ZERO: Self = Self { nanos: 0 }; |
| 172 | + |
| 173 | + /// Create a new [`Delta`] from a number of microseconds. |
| 174 | + /// |
| 175 | + /// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_036_854_775. |
| 176 | + /// If `micros` is outside this range, `i64::MIN` is used for negative values, |
| 177 | + /// and `i64::MAX` is used for positive values due to saturation. |
| 178 | + #[inline] |
| 179 | + pub const fn from_micros(micros: i64) -> Self { |
| 180 | + Self { |
| 181 | + nanos: micros.saturating_mul(NSEC_PER_USEC), |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /// Create a new [`Delta`] from a number of milliseconds. |
| 186 | + /// |
| 187 | + /// The `millis` can range from -9_223_372_036_854 to 9_223_372_036_854. |
| 188 | + /// If `millis` is outside this range, `i64::MIN` is used for negative values, |
| 189 | + /// and `i64::MAX` is used for positive values due to saturation. |
| 190 | + #[inline] |
| 191 | + pub const fn from_millis(millis: i64) -> Self { |
| 192 | + Self { |
| 193 | + nanos: millis.saturating_mul(NSEC_PER_MSEC), |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /// Create a new [`Delta`] from a number of seconds. |
| 198 | + /// |
| 199 | + /// The `secs` can range from -9_223_372_036 to 9_223_372_036. |
| 200 | + /// If `secs` is outside this range, `i64::MIN` is used for negative values, |
| 201 | + /// and `i64::MAX` is used for positive values due to saturation. |
| 202 | + #[inline] |
| 203 | + pub const fn from_secs(secs: i64) -> Self { |
| 204 | + Self { |
| 205 | + nanos: secs.saturating_mul(NSEC_PER_SEC), |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /// Return `true` if the [`Delta`] spans no time. |
| 210 | + #[inline] |
| 211 | + pub fn is_zero(self) -> bool { |
| 212 | + self.as_nanos() == 0 |
| 213 | + } |
| 214 | + |
| 215 | + /// Return `true` if the [`Delta`] spans a negative amount of time. |
| 216 | + #[inline] |
| 217 | + pub fn is_negative(self) -> bool { |
| 218 | + self.as_nanos() < 0 |
| 219 | + } |
| 220 | + |
| 221 | + /// Return the number of nanoseconds in the [`Delta`]. |
| 222 | + #[inline] |
| 223 | + pub const fn as_nanos(self) -> i64 { |
| 224 | + self.nanos |
| 225 | + } |
| 226 | + |
| 227 | + /// Return the smallest number of microseconds greater than or equal |
| 228 | + /// to the value in the [`Delta`]. |
| 229 | + #[inline] |
| 230 | + pub const fn as_micros_ceil(self) -> i64 { |
| 231 | + self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC |
| 232 | + } |
| 233 | + |
| 234 | + /// Return the number of milliseconds in the [`Delta`]. |
| 235 | + #[inline] |
| 236 | + pub const fn as_millis(self) -> i64 { |
| 237 | + self.as_nanos() / NSEC_PER_MSEC |
| 238 | + } |
| 239 | +} |
0 commit comments