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 <pluth@0t.re>
This commit is contained in:
bors[bot] 2023-02-23 18:33:47 +00:00 committed by GitHub
commit 2209bef4f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -192,3 +192,19 @@ impl<'a> fmt::Display for Duration {
const fn div_ceil(num: u64, den: u64) -> u64 {
(num + den - 1) / den
}
impl TryFrom<core::time::Duration> for Duration {
type Error = <u64 as TryFrom<u128>>::Error;
/// Converts using [`Duration::from_micros`]. Fails if value can not be represented as u64.
fn try_from(value: core::time::Duration) -> Result<Self, Self::Error> {
Ok(Self::from_micros(value.as_micros().try_into()?))
}
}
impl From<Duration> for core::time::Duration {
/// Converts using [`Duration::as_micros`].
fn from(value: Duration) -> Self {
core::time::Duration::from_micros(value.as_micros())
}
}