1 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
2 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4 // You may not use this file except in accordance with one or both of these
7 //! [`Time`] trait and different implementations. Currently, it's mainly used in tests so we can
8 //! manually advance time.
9 //! Other crates may symlink this file to use it while [`Time`] trait is sealed here.
12 use core::time::Duration;
14 /// A measurement of time.
15 pub trait Time: Copy + Sub<Duration, Output = Self> where Self: Sized {
16 /// Returns an instance corresponding to the current moment.
19 /// Returns the amount of time elapsed since `self` was created.
20 fn elapsed(&self) -> Duration;
22 /// Returns the amount of time passed between `earlier` and `self`.
23 fn duration_since(&self, earlier: Self) -> Duration;
25 /// Returns the amount of time passed since the beginning of [`Time`].
27 /// Used during (de-)serialization.
28 fn duration_since_epoch() -> Duration;
31 /// A state in which time has no meaning.
32 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
35 impl Time for Eternity {
40 fn duration_since(&self, _earlier: Self) -> Duration {
41 Duration::from_secs(0)
44 fn duration_since_epoch() -> Duration {
45 Duration::from_secs(0)
48 fn elapsed(&self) -> Duration {
49 Duration::from_secs(0)
53 impl Sub<Duration> for Eternity {
56 fn sub(self, _other: Duration) -> Self {
61 #[cfg(not(feature = "no-std"))]
62 impl Time for std::time::Instant {
64 std::time::Instant::now()
67 fn duration_since(&self, earlier: Self) -> Duration {
68 self.duration_since(earlier)
71 fn duration_since_epoch() -> Duration {
72 use std::time::SystemTime;
73 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap()
75 fn elapsed(&self) -> Duration {
76 std::time::Instant::elapsed(self)
82 use super::{Time, Eternity};
84 use core::time::Duration;
88 /// Time that can be advanced manually in tests.
89 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
90 pub struct SinceEpoch(Duration);
94 static ELAPSED: Cell<Duration> = core::cell::Cell::new(Duration::from_secs(0));
97 pub fn advance(duration: Duration) {
98 Self::ELAPSED.with(|elapsed| elapsed.set(elapsed.get() + duration))
102 impl Time for SinceEpoch {
104 Self(Self::duration_since_epoch())
107 fn duration_since(&self, earlier: Self) -> Duration {
111 fn duration_since_epoch() -> Duration {
112 Self::ELAPSED.with(|elapsed| elapsed.get())
115 fn elapsed(&self) -> Duration {
116 Self::duration_since_epoch() - self.0
120 impl Sub<Duration> for SinceEpoch {
123 fn sub(self, other: Duration) -> Self {
129 fn time_passes_when_advanced() {
130 let now = SinceEpoch::now();
131 assert_eq!(now.elapsed(), Duration::from_secs(0));
133 SinceEpoch::advance(Duration::from_secs(1));
134 SinceEpoch::advance(Duration::from_secs(1));
136 let elapsed = now.elapsed();
137 let later = SinceEpoch::now();
139 assert_eq!(elapsed, Duration::from_secs(2));
140 assert_eq!(later - elapsed, now);
144 fn time_never_passes_in_an_eternity() {
145 let now = Eternity::now();
146 let elapsed = now.elapsed();
147 let later = Eternity::now();
149 assert_eq!(now.elapsed(), Duration::from_secs(0));
150 assert_eq!(later - elapsed, now);