use core::{fmt, time::Duration};
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Moment(u64);
impl Moment {
pub const ORIGIN: Self = Self(0);
pub const fn from_nanos(nanos: u64) -> Self {
Self(nanos)
}
pub const fn as_nanos(self) -> u64 {
self.0
}
pub const fn checked_add(self, span: Span) -> Option<Self> {
match self.0.checked_add(span.0) {
Some(value) => Some(Self(value)),
None => None,
}
}
pub const fn duration_since(self, earlier: Self) -> Option<Span> {
match self.0.checked_sub(earlier.0) {
Some(value) => Some(Span(value)),
None => None,
}
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Span(u64);
impl Span {
pub const ZERO: Self = Self(0);
pub const fn from_nanos(nanos: u64) -> Self {
Self(nanos)
}
pub const fn checked_from_millis(milliseconds: u64) -> Option<Self> {
match milliseconds.checked_mul(1_000_000) {
Some(value) => Some(Self(value)),
None => None,
}
}
pub const fn as_nanos(self) -> u64 {
self.0
}
pub const fn as_duration(self) -> Duration {
Duration::from_nanos(self.0)
}
pub const fn checked_add(self, other: Self) -> Option<Self> {
match self.0.checked_add(other.0) {
Some(value) => Some(Self(value)),
None => None,
}
}
#[must_use]
pub const fn min(self, other: Self) -> Self {
if self.0 <= other.0 { self } else { other }
}
}
impl TryFrom<Duration> for Span {
type Error = DurationOverflow;
fn try_from(duration: Duration) -> Result<Self, Self::Error> {
let nanos =
u64::try_from(duration.as_nanos()).map_err(|_| DurationOverflow { duration })?;
Ok(Self(nanos))
}
}
impl From<Span> for Duration {
fn from(span: Span) -> Self {
span.as_duration()
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Deadline(Moment);
impl Deadline {
pub const fn at(moment: Moment) -> Self {
Self(moment)
}
pub const fn moment(self) -> Moment {
self.0
}
pub const fn is_elapsed_at(self, now: Moment) -> bool {
self.0.0 <= now.0
}
pub const fn remaining_at(self, now: Moment) -> Span {
match self.0.0.checked_sub(now.0) {
Some(value) => Span(value),
None => Span::ZERO,
}
}
}
impl From<Moment> for Deadline {
fn from(moment: Moment) -> Self {
Self::at(moment)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DurationOverflow {
duration: Duration,
}
impl DurationOverflow {
pub const fn duration(self) -> Duration {
self.duration
}
}
impl fmt::Display for DurationOverflow {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"duration {:?} exceeds the u64 nanosecond time domain",
self.duration
)
}
}
impl core::error::Error for DurationOverflow {}