From 907008df6695b5ee3d1b54afb8ec6d36ed42b3c2 Mon Sep 17 00:00:00 2001 From: Mathieu Trossevin Date: Sat, 6 Jan 2024 14:45:36 +0100 Subject: [PATCH] notify: Use Duration inside the newtype Microseconds --- src/notify/error.rs | 15 +++++++++++++++ src/notify/mod.rs | 12 ++++++------ src/notify/types.rs | 21 ++++++++++++++------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/notify/error.rs b/src/notify/error.rs index 41871a2..d64b824 100644 --- a/src/notify/error.rs +++ b/src/notify/error.rs @@ -121,3 +121,18 @@ impl Display for OtherStateError { } impl Error for OtherStateError {} + +#[derive(Debug)] +pub enum MicrosecondsFromDurationError { + NoMicroseconds, +} + +impl Display for MicrosecondsFromDurationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::NoMicroseconds => write!(f, "Minimal precision is microseconds but no microseconds are within the Duration provided."), + } + } +} + +impl Error for MicrosecondsFromDurationError {} diff --git a/src/notify/mod.rs b/src/notify/mod.rs index 557ae93..7538dbc 100644 --- a/src/notify/mod.rs +++ b/src/notify/mod.rs @@ -230,10 +230,10 @@ pub enum NotifyState<'a> { /// Tell the service manager to execute the configured watchdog option. WatchdogTrigger, /// Reset watchdog timeout value during runtime. - /// The value is in microseconds. + /// Minimal precision is microseconds, not nanoseconds. WatchdogUsec(types::Microseconds), /// Tells the service manager to extend the startup, runtime or shutdown service timeout corresponding the current state. - /// The value is in microseconds. + /// Minimal precision is microseconds, not nanoseconds. ExtendTimeoutUsec(types::Microseconds), } @@ -254,11 +254,11 @@ impl<'a> Display for NotifyState<'a> { NotifyState::Stopping => f.write_str("STOPPING=1"), NotifyState::Watchdog => f.write_str("WATCHDOG=1"), NotifyState::WatchdogTrigger => f.write_str("WATCHDOG=trigger"), - NotifyState::WatchdogUsec(types::Microseconds(milliseconds)) => { - write!(f, "WATCHDOG_USEC={milliseconds}") + NotifyState::WatchdogUsec(duration) => { + write!(f, "WATCHDOG_USEC={duration}") } - NotifyState::ExtendTimeoutUsec(types::Microseconds(milliseconds)) => { - write!(f, "EXTEND_TIMEOUT_USEC={milliseconds}") + NotifyState::ExtendTimeoutUsec(duration) => { + write!(f, "EXTEND_TIMEOUT_USEC={duration}") } } } diff --git a/src/notify/types.rs b/src/notify/types.rs index d260d16..b87dc91 100644 --- a/src/notify/types.rs +++ b/src/notify/types.rs @@ -1,5 +1,7 @@ //! Newtypes used by [`NotifyState`](super::NotifyState) +use core::{fmt::Display, time::Duration}; + use super::error; /// Allowed File descriptor name. @@ -72,17 +74,22 @@ impl AsRef for StatusLine<'_> { /// Semantic type representing a number of microseconds. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Microseconds(pub(super) u64); +pub struct Microseconds(Duration); -impl From for Microseconds { - fn from(value: u64) -> Self { - Self(value) +impl TryFrom for Microseconds { + type Error = error::MicrosecondsFromDurationError; + + fn try_from(value: Duration) -> Result { + if value.as_micros() == 0 { + return Err(Self::Error::NoMicroseconds); + } + Ok(Self(value)) } } -impl AsRef for Microseconds { - fn as_ref(&self) -> &u64 { - &self.0 +impl Display for Microseconds { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0.as_micros()) } }