Compare commits

...

2 commits

Author SHA1 Message Date
Mathieu Trossevin eb5dc84ca8 Merge branch 'main' into notify_barrier 2024-01-06 15:04:15 +01:00
Mathieu Trossevin 907008df66 notify: Use Duration inside the newtype Microseconds 2024-01-06 14:56:41 +01:00
3 changed files with 31 additions and 18 deletions

View file

@ -175,3 +175,18 @@ impl Display for PollTimeoutFromIntError {
} }
impl Error for PollTimeoutFromIntError {} impl Error for PollTimeoutFromIntError {}
#[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 {}

View file

@ -349,10 +349,10 @@ pub enum NotifyState<'a> {
/// Tell the service manager to execute the configured watchdog option. /// Tell the service manager to execute the configured watchdog option.
WatchdogTrigger, WatchdogTrigger,
/// Reset watchdog timeout value during runtime. /// Reset watchdog timeout value during runtime.
/// The value is in microseconds. /// Minimal precision is microseconds, not nanoseconds.
WatchdogUsec(types::Microseconds), WatchdogUsec(types::Microseconds),
/// Tells the service manager to extend the startup, runtime or shutdown service timeout corresponding the current state. /// 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), ExtendTimeoutUsec(types::Microseconds),
} }
@ -373,11 +373,11 @@ impl<'a> Display for NotifyState<'a> {
NotifyState::Stopping => f.write_str("STOPPING=1"), NotifyState::Stopping => f.write_str("STOPPING=1"),
NotifyState::Watchdog => f.write_str("WATCHDOG=1"), NotifyState::Watchdog => f.write_str("WATCHDOG=1"),
NotifyState::WatchdogTrigger => f.write_str("WATCHDOG=trigger"), NotifyState::WatchdogTrigger => f.write_str("WATCHDOG=trigger"),
NotifyState::WatchdogUsec(milliseconds) => { NotifyState::WatchdogUsec(duration) => {
write!(f, "WATCHDOG_USEC={milliseconds}") write!(f, "WATCHDOG_USEC={duration}")
} }
NotifyState::ExtendTimeoutUsec(milliseconds) => { NotifyState::ExtendTimeoutUsec(duration) => {
write!(f, "EXTEND_TIMEOUT_USEC={milliseconds}") write!(f, "EXTEND_TIMEOUT_USEC={duration}")
} }
} }
} }

View file

@ -1,6 +1,6 @@
//! Newtypes used by [`NotifyState`](super::NotifyState) //! Newtypes used by [`NotifyState`](super::NotifyState)
use core::fmt::Display; use core::{fmt::Display, time::Duration};
use super::error; use super::error;
@ -88,24 +88,22 @@ impl Display for StatusLine<'_> {
/// Semantic type representing a number of microseconds. /// Semantic type representing a number of microseconds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Microseconds(u64); pub struct Microseconds(Duration);
impl From<u64> for Microseconds { impl TryFrom<Duration> for Microseconds {
fn from(value: u64) -> Self { type Error = error::MicrosecondsFromDurationError;
Self(value)
}
}
impl From<Microseconds> for u64 { fn try_from(value: Duration) -> Result<Self, Self::Error> {
fn from(value: Microseconds) -> Self { if value.as_micros() == 0 {
value.0 return Err(Self::Error::NoMicroseconds);
}
Ok(Self(value))
} }
} }
impl Display for Microseconds { impl Display for Microseconds {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f) write!(f, "{}", self.0.as_micros())
} }
} }