From d99cab5f27505cd336600b31ebc16fc4a111e701 Mon Sep 17 00:00:00 2001 From: Scott Lamb Date: Thu, 22 Apr 2021 09:56:01 -0700 Subject: [PATCH] std::time::Duration -> base::Duration conversion --- server/base/time.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/server/base/time.rs b/server/base/time.rs index e3959eb..f8b260c 100644 --- a/server/base/time.rs +++ b/server/base/time.rs @@ -289,6 +289,14 @@ impl fmt::Display for Duration { } } +impl std::convert::TryFrom for Duration { + type Error = std::num::TryFromIntError; + + fn try_from(value: std::time::Duration) -> Result { + Ok(Self(i64::try_from(value.as_nanos() * 9 / 100_000)?)) + } +} + impl ops::Mul for Duration { type Output = Self; fn mul(self, rhs: i64) -> Self::Output { @@ -318,6 +326,7 @@ impl ops::SubAssign for Duration { #[cfg(test)] mod tests { use super::{Duration, Time, TIME_UNITS_PER_SEC}; + use std::convert::TryFrom; #[test] fn test_parse_time() { @@ -373,4 +382,21 @@ mod tests { assert_eq!(test.0, format!("{}", Duration(test.1 * TIME_UNITS_PER_SEC))); } } + + #[test] + fn test_duration_from_std_duration() { + assert_eq!( + Duration::try_from(std::time::Duration::new(1, 11111)), + Ok(Duration(90_000)) + ); + assert_eq!( + Duration::try_from(std::time::Duration::new(1, 11112)), + Ok(Duration(90_001)) + ); + assert_eq!( + Duration::try_from(std::time::Duration::new(60, 0)), + Ok(Duration(60 * TIME_UNITS_PER_SEC)) + ); + Duration::try_from(std::time::Duration::new(u64::MAX, 0)).unwrap_err(); + } }