+pub(crate) mod time {
+ use core::ops::Sub;
+ use core::time::Duration;
+ /// A measurement of time.
+ pub trait Time: Sub<Duration, Output = Self> where Self: Sized {
+ /// Returns an instance corresponding to the current moment.
+ fn now() -> Self;
+
+ /// Returns the amount of time elapsed since `self` was created.
+ fn elapsed(&self) -> Duration;
+
+ /// Returns the amount of time passed since the beginning of [`Time`].
+ ///
+ /// Used during (de-)serialization.
+ fn duration_since_epoch() -> Duration;
+ }
+
+ /// A state in which time has no meaning.
+ #[derive(Debug, PartialEq, Eq)]
+ pub struct Eternity;
+
+ #[cfg(not(feature = "no-std"))]
+ impl Time for std::time::Instant {
+ fn now() -> Self {
+ std::time::Instant::now()
+ }
+
+ fn duration_since_epoch() -> Duration {
+ use std::time::SystemTime;
+ SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
+ }
+
+ fn elapsed(&self) -> Duration {
+ std::time::Instant::elapsed(self)
+ }
+ }
+
+ impl Time for Eternity {
+ fn now() -> Self {
+ Self
+ }
+
+ fn duration_since_epoch() -> Duration {
+ Duration::from_secs(0)
+ }
+
+ fn elapsed(&self) -> Duration {
+ Duration::from_secs(0)
+ }
+ }
+
+ impl Sub<Duration> for Eternity {
+ type Output = Self;
+
+ fn sub(self, _other: Duration) -> Self {
+ self
+ }
+ }
+}
+
+pub(crate) use self::time::Time;
+