diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-02-23 18:33:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-23 18:33:47 +0000 |
| commit | 2209bef4f22bf77d9d52cda0a0ea40485cc2747a (patch) | |
| tree | 7ee9498df9dc51412c2bf7dc0d265dbb6ef2d80d /embassy-time/src | |
| parent | 3255e0a17200774a6b434e9e1bd2a0f2dc2be6bd (diff) | |
| parent | 43a4409405e0df2f53452ca5d3a19a7d13e0f234 (diff) | |
Merge #1231
1231: embassy-time: Implement conversions to/from core::time::Duration for embassy-time::Duration r=Dirbaio a=kbleeke
I chose microseconds for the conversion as the lowest resolution that embassy provides. A new Error-type did not seem that useful but I can add one, if necessary.
Co-authored-by: kbleeke <[email protected]>
Diffstat (limited to 'embassy-time/src')
| -rw-r--r-- | embassy-time/src/duration.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs index 9d0bab2dd..8366455be 100644 --- a/embassy-time/src/duration.rs +++ b/embassy-time/src/duration.rs | |||
| @@ -192,3 +192,19 @@ impl<'a> fmt::Display for Duration { | |||
| 192 | const fn div_ceil(num: u64, den: u64) -> u64 { | 192 | const fn div_ceil(num: u64, den: u64) -> u64 { |
| 193 | (num + den - 1) / den | 193 | (num + den - 1) / den |
| 194 | } | 194 | } |
| 195 | |||
| 196 | impl TryFrom<core::time::Duration> for Duration { | ||
| 197 | type Error = <u64 as TryFrom<u128>>::Error; | ||
| 198 | |||
| 199 | /// Converts using [`Duration::from_micros`]. Fails if value can not be represented as u64. | ||
| 200 | fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> { | ||
| 201 | Ok(Self::from_micros(value.as_micros().try_into()?)) | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | impl From<Duration> for core::time::Duration { | ||
| 206 | /// Converts using [`Duration::as_micros`]. | ||
| 207 | fn from(value: Duration) -> Self { | ||
| 208 | core::time::Duration::from_micros(value.as_micros()) | ||
| 209 | } | ||
| 210 | } | ||
