diff options
Diffstat (limited to 'embassy-executor/src/time/duration.rs')
| -rw-r--r-- | embassy-executor/src/time/duration.rs | 184 |
1 files changed, 0 insertions, 184 deletions
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 @@ | |||
| 1 | use core::fmt; | ||
| 2 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; | ||
| 3 | |||
| 4 | use super::{GCD_1K, GCD_1M, TICKS_PER_SECOND}; | ||
| 5 | |||
| 6 | #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
| 7 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 8 | /// Represents the difference between two [Instant](struct.Instant.html)s | ||
| 9 | pub struct Duration { | ||
| 10 | pub(crate) ticks: u64, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl Duration { | ||
| 14 | /// The smallest value that can be represented by the `Duration` type. | ||
| 15 | pub const MIN: Duration = Duration { ticks: u64::MIN }; | ||
| 16 | /// The largest value that can be represented by the `Duration` type. | ||
| 17 | pub const MAX: Duration = Duration { ticks: u64::MAX }; | ||
| 18 | |||
| 19 | /// Tick count of the `Duration`. | ||
| 20 | pub const fn as_ticks(&self) -> u64 { | ||
| 21 | self.ticks | ||
| 22 | } | ||
| 23 | |||
| 24 | /// Convert the `Duration` to seconds, rounding down. | ||
| 25 | pub const fn as_secs(&self) -> u64 { | ||
| 26 | self.ticks / TICKS_PER_SECOND | ||
| 27 | } | ||
| 28 | |||
| 29 | /// Convert the `Duration` to milliseconds, rounding down. | ||
| 30 | pub const fn as_millis(&self) -> u64 { | ||
| 31 | self.ticks * (1000 / GCD_1K) / (TICKS_PER_SECOND / GCD_1K) | ||
| 32 | } | ||
| 33 | |||
| 34 | /// Convert the `Duration` to microseconds, rounding down. | ||
| 35 | pub const fn as_micros(&self) -> u64 { | ||
| 36 | self.ticks * (1_000_000 / GCD_1M) / (TICKS_PER_SECOND / GCD_1M) | ||
| 37 | } | ||
| 38 | |||
| 39 | /// Creates a duration from the specified number of clock ticks | ||
| 40 | pub const fn from_ticks(ticks: u64) -> Duration { | ||
| 41 | Duration { ticks } | ||
| 42 | } | ||
| 43 | |||
| 44 | /// Creates a duration from the specified number of seconds, rounding up. | ||
| 45 | pub const fn from_secs(secs: u64) -> Duration { | ||
| 46 | Duration { | ||
| 47 | ticks: secs * TICKS_PER_SECOND, | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | /// Creates a duration from the specified number of milliseconds, rounding up. | ||
| 52 | pub const fn from_millis(millis: u64) -> Duration { | ||
| 53 | Duration { | ||
| 54 | ticks: div_ceil(millis * (TICKS_PER_SECOND / GCD_1K), 1000 / GCD_1K), | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | /// Creates a duration from the specified number of microseconds, rounding up. | ||
| 59 | /// NOTE: Delays this small may be inaccurate. | ||
| 60 | pub const fn from_micros(micros: u64) -> Duration { | ||
| 61 | Duration { | ||
| 62 | ticks: div_ceil(micros * (TICKS_PER_SECOND / GCD_1M), 1_000_000 / GCD_1M), | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | /// Creates a duration from the specified number of seconds, rounding down. | ||
| 67 | pub const fn from_secs_floor(secs: u64) -> Duration { | ||
| 68 | Duration { | ||
| 69 | ticks: secs * TICKS_PER_SECOND, | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | /// Creates a duration from the specified number of milliseconds, rounding down. | ||
| 74 | pub const fn from_millis_floor(millis: u64) -> Duration { | ||
| 75 | Duration { | ||
| 76 | ticks: millis * (TICKS_PER_SECOND / GCD_1K) / (1000 / GCD_1K), | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | /// Creates a duration from the specified number of microseconds, rounding down. | ||
| 81 | /// NOTE: Delays this small may be inaccurate. | ||
| 82 | pub const fn from_micros_floor(micros: u64) -> Duration { | ||
| 83 | Duration { | ||
| 84 | ticks: micros * (TICKS_PER_SECOND / GCD_1M) / (1_000_000 / GCD_1M), | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | /// Adds one Duration to another, returning a new Duration or None in the event of an overflow. | ||
| 89 | pub fn checked_add(self, rhs: Duration) -> Option<Duration> { | ||
| 90 | self.ticks.checked_add(rhs.ticks).map(|ticks| Duration { ticks }) | ||
| 91 | } | ||
| 92 | |||
| 93 | /// Subtracts one Duration to another, returning a new Duration or None in the event of an overflow. | ||
| 94 | pub fn checked_sub(self, rhs: Duration) -> Option<Duration> { | ||
| 95 | self.ticks.checked_sub(rhs.ticks).map(|ticks| Duration { ticks }) | ||
| 96 | } | ||
| 97 | |||
| 98 | /// Multiplies one Duration by a scalar u32, returning a new Duration or None in the event of an overflow. | ||
| 99 | pub fn checked_mul(self, rhs: u32) -> Option<Duration> { | ||
| 100 | self.ticks.checked_mul(rhs as _).map(|ticks| Duration { ticks }) | ||
| 101 | } | ||
| 102 | |||
| 103 | /// Divides one Duration a scalar u32, returning a new Duration or None in the event of an overflow. | ||
| 104 | pub fn checked_div(self, rhs: u32) -> Option<Duration> { | ||
| 105 | self.ticks.checked_div(rhs as _).map(|ticks| Duration { ticks }) | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | impl Add for Duration { | ||
| 110 | type Output = Duration; | ||
| 111 | |||
| 112 | fn add(self, rhs: Duration) -> Duration { | ||
| 113 | self.checked_add(rhs).expect("overflow when adding durations") | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | impl AddAssign for Duration { | ||
| 118 | fn add_assign(&mut self, rhs: Duration) { | ||
| 119 | *self = *self + rhs; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | impl Sub for Duration { | ||
| 124 | type Output = Duration; | ||
| 125 | |||
| 126 | fn sub(self, rhs: Duration) -> Duration { | ||
| 127 | self.checked_sub(rhs).expect("overflow when subtracting durations") | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | impl SubAssign for Duration { | ||
| 132 | fn sub_assign(&mut self, rhs: Duration) { | ||
| 133 | *self = *self - rhs; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | impl Mul<u32> for Duration { | ||
| 138 | type Output = Duration; | ||
| 139 | |||
| 140 | fn mul(self, rhs: u32) -> Duration { | ||
| 141 | self.checked_mul(rhs) | ||
| 142 | .expect("overflow when multiplying duration by scalar") | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | impl Mul<Duration> for u32 { | ||
| 147 | type Output = Duration; | ||
| 148 | |||
| 149 | fn mul(self, rhs: Duration) -> Duration { | ||
| 150 | rhs * self | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | impl MulAssign<u32> for Duration { | ||
| 155 | fn mul_assign(&mut self, rhs: u32) { | ||
| 156 | *self = *self * rhs; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | impl Div<u32> for Duration { | ||
| 161 | type Output = Duration; | ||
| 162 | |||
| 163 | fn div(self, rhs: u32) -> Duration { | ||
| 164 | self.checked_div(rhs) | ||
| 165 | .expect("divide by zero error when dividing duration by scalar") | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | impl DivAssign<u32> for Duration { | ||
| 170 | fn div_assign(&mut self, rhs: u32) { | ||
| 171 | *self = *self / rhs; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | impl<'a> fmt::Display for Duration { | ||
| 176 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| 177 | write!(f, "{} ticks", self.ticks) | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | #[inline] | ||
| 182 | const fn div_ceil(num: u64, den: u64) -> u64 { | ||
| 183 | (num + den - 1) / den | ||
| 184 | } | ||
