From 43a4409405e0df2f53452ca5d3a19a7d13e0f234 Mon Sep 17 00:00:00 2001 From: kbleeke Date: Thu, 23 Feb 2023 19:25:22 +0100 Subject: [PATCH] embassy-time: Implement conversions to/from core::time::Duration for embassy-time::Duration --- embassy-time/src/duration.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 { const fn div_ceil(num: u64, den: u64) -> u64 { (num + den - 1) / den } + +impl TryFrom for Duration { + type Error = >::Error; + + /// Converts using [`Duration::from_micros`]. Fails if value can not be represented as u64. + fn try_from(value: core::time::Duration) -> Result { + Ok(Self::from_micros(value.as_micros().try_into()?)) + } +} + +impl From for core::time::Duration { + /// Converts using [`Duration::as_micros`]. + fn from(value: Duration) -> Self { + core::time::Duration::from_micros(value.as_micros()) + } +}