From 5daa173ce4b153a532b4daa9e94c7a248231f25b Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 17 Aug 2022 23:40:16 +0200 Subject: Split embassy-time from embassy-executor. --- embassy-executor/src/time/duration.rs | 184 ---------------------------------- 1 file changed, 184 deletions(-) delete mode 100644 embassy-executor/src/time/duration.rs (limited to 'embassy-executor/src/time/duration.rs') diff --git a/embassy-executor/src/time/duration.rs b/embassy-executor/src/time/duration.rs deleted file mode 100644 index dc4f16bd4..000000000 --- a/embassy-executor/src/time/duration.rs +++ /dev/null @@ -1,184 +0,0 @@ -use core::fmt; -use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; - -use super::{GCD_1K, GCD_1M, TICKS_PER_SECOND}; - -#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -/// Represents the difference between two [Instant](struct.Instant.html)s -pub struct Duration { - pub(crate) ticks: u64, -} - -impl Duration { - /// 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`. - pub const fn as_ticks(&self) -> u64 { - self.ticks - } - - /// Convert the `Duration` to seconds, rounding down. - pub const fn as_secs(&self) -> u64 { - self.ticks / TICKS_PER_SECOND - } - - /// Convert the `Duration` to milliseconds, rounding down. - pub const fn as_millis(&self) -> u64 { - self.ticks * (1000 / GCD_1K) / (TICKS_PER_SECOND / GCD_1K) - } - - /// Convert the `Duration` to microseconds, rounding down. - pub const fn as_micros(&self) -> u64 { - self.ticks * (1_000_000 / GCD_1M) / (TICKS_PER_SECOND / GCD_1M) - } - - /// Creates a duration from the specified number of clock ticks - pub const fn from_ticks(ticks: u64) -> Duration { - Duration { ticks } - } - - /// Creates a duration from the specified number of seconds, rounding up. - pub const fn from_secs(secs: u64) -> Duration { - Duration { - ticks: secs * TICKS_PER_SECOND, - } - } - - /// Creates a duration from the specified number of milliseconds, rounding up. - pub const fn from_millis(millis: u64) -> Duration { - Duration { - ticks: div_ceil(millis * (TICKS_PER_SECOND / GCD_1K), 1000 / GCD_1K), - } - } - - /// Creates a duration from the specified number of microseconds, rounding up. - /// NOTE: Delays this small may be inaccurate. - pub const fn from_micros(micros: u64) -> Duration { - Duration { - ticks: div_ceil(micros * (TICKS_PER_SECOND / GCD_1M), 1_000_000 / GCD_1M), - } - } - - /// Creates a duration from the specified number of seconds, rounding down. - pub const fn from_secs_floor(secs: u64) -> Duration { - Duration { - ticks: secs * TICKS_PER_SECOND, - } - } - - /// Creates a duration from the specified number of milliseconds, rounding down. - pub const fn from_millis_floor(millis: u64) -> Duration { - Duration { - ticks: millis * (TICKS_PER_SECOND / GCD_1K) / (1000 / GCD_1K), - } - } - - /// 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 { - Duration { - ticks: micros * (TICKS_PER_SECOND / GCD_1M) / (1_000_000 / GCD_1M), - } - } - - /// Adds one Duration to another, returning a new Duration or None in the event of an overflow. - pub fn checked_add(self, rhs: Duration) -> Option { - self.ticks.checked_add(rhs.ticks).map(|ticks| Duration { ticks }) - } - - /// Subtracts one Duration to another, returning a new Duration or None in the event of an overflow. - pub fn checked_sub(self, rhs: Duration) -> Option { - self.ticks.checked_sub(rhs.ticks).map(|ticks| Duration { ticks }) - } - - /// Multiplies one Duration by a scalar u32, returning a new Duration or None in the event of an overflow. - pub fn checked_mul(self, rhs: u32) -> Option { - self.ticks.checked_mul(rhs as _).map(|ticks| Duration { ticks }) - } - - /// Divides one Duration a scalar u32, returning a new Duration or None in the event of an overflow. - pub fn checked_div(self, rhs: u32) -> Option { - self.ticks.checked_div(rhs as _).map(|ticks| Duration { ticks }) - } -} - -impl Add for Duration { - type Output = Duration; - - fn add(self, rhs: Duration) -> Duration { - self.checked_add(rhs).expect("overflow when adding durations") - } -} - -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 { - self.checked_sub(rhs).expect("overflow when subtracting durations") - } -} - -impl SubAssign for Duration { - fn sub_assign(&mut self, rhs: Duration) { - *self = *self - rhs; - } -} - -impl Mul for Duration { - type Output = Duration; - - fn mul(self, rhs: u32) -> Duration { - self.checked_mul(rhs) - .expect("overflow when multiplying duration by scalar") - } -} - -impl Mul for u32 { - type Output = Duration; - - fn mul(self, rhs: Duration) -> Duration { - rhs * self - } -} - -impl MulAssign for Duration { - fn mul_assign(&mut self, rhs: u32) { - *self = *self * rhs; - } -} - -impl Div 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 for Duration { - fn div_assign(&mut self, rhs: u32) { - *self = *self / rhs; - } -} - -impl<'a> fmt::Display for Duration { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} ticks", self.ticks) - } -} - -#[inline] -const fn div_ceil(num: u64, den: u64) -> u64 { - (num + den - 1) / den -} -- cgit