2020-10-26 12:39:53 +01:00
|
|
|
use core::fmt;
|
2020-10-31 20:02:16 +01:00
|
|
|
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2022-09-02 00:58:31 +02:00
|
|
|
use super::{GCD_1K, GCD_1M, TICK_HZ};
|
2020-10-19 21:15:24 +02:00
|
|
|
|
2020-12-01 17:46:56 +01:00
|
|
|
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-03-22 01:11:30 +01:00
|
|
|
/// Represents the difference between two [Instant](struct.Instant.html)s
|
2020-10-19 21:15:24 +02:00
|
|
|
pub struct Duration {
|
2020-10-26 12:39:53 +01:00
|
|
|
pub(crate) ticks: u64,
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Duration {
|
2021-08-24 22:46:07 +02:00
|
|
|
/// The smallest value that can be represented by the `Duration` type.
|
|
|
|
pub const MIN: Duration = Duration { ticks: u64::MIN };
|
|
|
|
/// The largest value that can be represented by the `Duration` type.
|
|
|
|
pub const MAX: Duration = Duration { ticks: u64::MAX };
|
|
|
|
|
|
|
|
/// Tick count of the `Duration`.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn as_ticks(&self) -> u64 {
|
2020-10-19 21:15:24 +02:00
|
|
|
self.ticks
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Convert the `Duration` to seconds, rounding down.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn as_secs(&self) -> u64 {
|
2022-09-02 00:58:31 +02:00
|
|
|
self.ticks / TICK_HZ
|
2020-10-26 12:39:53 +01:00
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Convert the `Duration` to milliseconds, rounding down.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn as_millis(&self) -> u64 {
|
2022-09-02 00:58:31 +02:00
|
|
|
self.ticks * (1000 / GCD_1K) / (TICK_HZ / GCD_1K)
|
2020-10-26 12:39:53 +01:00
|
|
|
}
|
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Convert the `Duration` to microseconds, rounding down.
|
2021-03-01 18:59:40 +01:00
|
|
|
pub const fn as_micros(&self) -> u64 {
|
2022-09-02 00:58:31 +02:00
|
|
|
self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
|
2021-03-01 18:59:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:45:48 +01:00
|
|
|
/// Creates a duration from the specified number of clock ticks
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn from_ticks(ticks: u64) -> Duration {
|
2020-10-19 21:15:24 +02:00
|
|
|
Duration { ticks }
|
|
|
|
}
|
|
|
|
|
2022-06-24 23:27:46 +02:00
|
|
|
/// Creates a duration from the specified number of seconds, rounding up.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn from_secs(secs: u64) -> Duration {
|
2022-09-02 00:58:31 +02:00
|
|
|
Duration { ticks: secs * TICK_HZ }
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
2021-03-22 01:01:13 +01:00
|
|
|
|
2022-06-24 23:27:46 +02:00
|
|
|
/// Creates a duration from the specified number of milliseconds, rounding up.
|
2020-10-26 12:39:53 +01:00
|
|
|
pub const fn from_millis(millis: u64) -> Duration {
|
2020-10-19 21:15:24 +02:00
|
|
|
Duration {
|
2022-09-02 00:58:31 +02:00
|
|
|
ticks: div_ceil(millis * (TICK_HZ / GCD_1K), 1000 / GCD_1K),
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 23:27:46 +02:00
|
|
|
/// Creates a duration from the specified number of microseconds, rounding up.
|
2021-03-22 00:45:48 +01:00
|
|
|
/// NOTE: Delays this small may be inaccurate.
|
2021-03-02 15:45:22 +01:00
|
|
|
pub const fn from_micros(micros: u64) -> Duration {
|
2022-06-24 23:27:46 +02:00
|
|
|
Duration {
|
2022-09-02 00:58:31 +02:00
|
|
|
ticks: div_ceil(micros * (TICK_HZ / GCD_1M), 1_000_000 / GCD_1M),
|
2022-06-24 23:27:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a duration from the specified number of seconds, rounding down.
|
|
|
|
pub const fn from_secs_floor(secs: u64) -> Duration {
|
2022-09-02 00:58:31 +02:00
|
|
|
Duration { ticks: secs * TICK_HZ }
|
2022-06-24 23:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a duration from the specified number of milliseconds, rounding down.
|
|
|
|
pub const fn from_millis_floor(millis: u64) -> Duration {
|
|
|
|
Duration {
|
2022-09-02 00:58:31 +02:00
|
|
|
ticks: millis * (TICK_HZ / GCD_1K) / (1000 / GCD_1K),
|
2022-06-24 23:27:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a duration from the specified number of microseconds, rounding down.
|
|
|
|
/// NOTE: Delays this small may be inaccurate.
|
|
|
|
pub const fn from_micros_floor(micros: u64) -> Duration {
|
2021-03-01 18:47:55 +01:00
|
|
|
Duration {
|
2022-09-02 00:58:31 +02:00
|
|
|
ticks: micros * (TICK_HZ / GCD_1M) / (1_000_000 / GCD_1M),
|
2021-03-01 18:47:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 01:22:06 +01:00
|
|
|
/// Creates a duration corresponding to the specified Hz.
|
|
|
|
pub const fn from_hz(hz: u64) -> Duration {
|
|
|
|
Duration { ticks: TICK_HZ / hz }
|
|
|
|
}
|
|
|
|
|
2021-03-22 00:45:48 +01:00
|
|
|
/// Adds one Duration to another, returning a new Duration or None in the event of an overflow.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn checked_add(self, rhs: Duration) -> Option<Duration> {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.ticks.checked_add(rhs.ticks).map(|ticks| Duration { ticks })
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
2021-03-22 01:01:13 +01:00
|
|
|
|
2021-03-22 00:45:48 +01:00
|
|
|
/// Subtracts one Duration to another, returning a new Duration or None in the event of an overflow.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn checked_sub(self, rhs: Duration) -> Option<Duration> {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.ticks.checked_sub(rhs.ticks).map(|ticks| Duration { ticks })
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 01:11:30 +01:00
|
|
|
/// Multiplies one Duration by a scalar u32, returning a new Duration or None in the event of an overflow.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn checked_mul(self, rhs: u32) -> Option<Duration> {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.ticks.checked_mul(rhs as _).map(|ticks| Duration { ticks })
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
2021-03-22 01:01:13 +01:00
|
|
|
|
2021-03-22 01:11:30 +01:00
|
|
|
/// Divides one Duration a scalar u32, returning a new Duration or None in the event of an overflow.
|
2020-10-19 21:15:24 +02:00
|
|
|
pub fn checked_div(self, rhs: u32) -> Option<Duration> {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.ticks.checked_div(rhs as _).map(|ticks| Duration { ticks })
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn add(self, rhs: Duration) -> Duration {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.checked_add(rhs).expect("overflow when adding durations")
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign for Duration {
|
|
|
|
fn add_assign(&mut self, rhs: Duration) {
|
|
|
|
*self = *self + rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub for Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Duration) -> Duration {
|
2022-06-12 22:15:44 +02:00
|
|
|
self.checked_sub(rhs).expect("overflow when subtracting durations")
|
2020-10-19 21:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SubAssign for Duration {
|
|
|
|
fn sub_assign(&mut self, rhs: Duration) {
|
|
|
|
*self = *self - rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul<u32> for Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn mul(self, rhs: u32) -> Duration {
|
|
|
|
self.checked_mul(rhs)
|
|
|
|
.expect("overflow when multiplying duration by scalar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul<Duration> for u32 {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn mul(self, rhs: Duration) -> Duration {
|
|
|
|
rhs * self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MulAssign<u32> for Duration {
|
|
|
|
fn mul_assign(&mut self, rhs: u32) {
|
|
|
|
*self = *self * rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Div<u32> for Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn div(self, rhs: u32) -> Duration {
|
|
|
|
self.checked_div(rhs)
|
|
|
|
.expect("divide by zero error when dividing duration by scalar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DivAssign<u32> for Duration {
|
|
|
|
fn div_assign(&mut self, rhs: u32) {
|
|
|
|
*self = *self / rhs;
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 12:39:53 +01:00
|
|
|
|
|
|
|
impl<'a> fmt::Display for Duration {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{} ticks", self.ticks)
|
|
|
|
}
|
|
|
|
}
|
2022-06-24 23:27:46 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
const fn div_ceil(num: u64, den: u64) -> u64 {
|
|
|
|
(num + den - 1) / den
|
|
|
|
}
|