use core::time::Duration;
use mdns_proto::Instant as ProtoInstant;
use smoltcp::time::Instant as RawInstant;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SmoltcpInstant(pub RawInstant);
impl From<RawInstant> for SmoltcpInstant {
#[inline(always)]
fn from(raw: RawInstant) -> Self {
Self(raw)
}
}
impl From<SmoltcpInstant> for RawInstant {
#[inline(always)]
fn from(wrapped: SmoltcpInstant) -> Self {
wrapped.0
}
}
impl ProtoInstant for SmoltcpInstant {
#[inline]
fn checked_add_duration(self, dur: Duration) -> Option<Self> {
let add_us = i64::try_from(dur.as_micros()).ok()?;
let total = self.0.total_micros().checked_add(add_us)?;
Some(Self(RawInstant::from_micros(total)))
}
#[inline]
fn checked_duration_since(self, earlier: Self) -> Option<Duration> {
let delta = self
.0
.total_micros()
.checked_sub(earlier.0.total_micros())?;
let micros = u64::try_from(delta).ok()?;
Some(Duration::from_micros(micros))
}
}
#[cfg(test)]
mod tests;