axstd/time.rs
1//! Temporal quantification.
2
3use arceos_api::time::AxTimeValue;
4use core::ops::{Add, AddAssign, Sub, SubAssign};
5
6pub use core::time::Duration;
7
8/// A measurement of a monotonically nondecreasing clock.
9/// Opaque and useful only with [`Duration`].
10#[derive(Clone, Copy)]
11pub struct Instant(AxTimeValue);
12
13impl Instant {
14 /// Returns an instant corresponding to "now".
15 pub fn now() -> Instant {
16 Instant(arceos_api::time::ax_wall_time())
17 }
18
19 /// Returns the amount of time elapsed from another instant to this one,
20 /// or zero duration if that instant is later than this one.
21 ///
22 /// # Panics
23 ///
24 /// Previous rust versions panicked when `earlier` was later than `self`. Currently this
25 /// method saturates. Future versions may reintroduce the panic in some circumstances.
26 pub fn duration_since(&self, earlier: Instant) -> Duration {
27 self.0.checked_sub(earlier.0).unwrap_or_default()
28 }
29
30 /// Returns the amount of time elapsed since this instant was created.
31 ///
32 /// # Panics
33 ///
34 /// Previous rust versions panicked when the current time was earlier than self. Currently this
35 /// method returns a Duration of zero in that case. Future versions may reintroduce the panic.
36 pub fn elapsed(&self) -> Duration {
37 Instant::now() - *self
38 }
39
40 /// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
41 /// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
42 /// otherwise.
43 pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
44 self.0.checked_add(duration).map(Instant)
45 }
46
47 /// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
48 /// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
49 /// otherwise.
50 pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
51 self.0.checked_sub(duration).map(Instant)
52 }
53}
54
55impl Add<Duration> for Instant {
56 type Output = Instant;
57
58 /// # Panics
59 ///
60 /// This function may panic if the resulting point in time cannot be represented by the
61 /// underlying data structure.
62 fn add(self, other: Duration) -> Instant {
63 self.checked_add(other)
64 .expect("overflow when adding duration to instant")
65 }
66}
67
68impl AddAssign<Duration> for Instant {
69 fn add_assign(&mut self, other: Duration) {
70 *self = *self + other;
71 }
72}
73
74impl Sub<Duration> for Instant {
75 type Output = Instant;
76
77 fn sub(self, other: Duration) -> Instant {
78 self.checked_sub(other)
79 .expect("overflow when subtracting duration from instant")
80 }
81}
82
83impl SubAssign<Duration> for Instant {
84 fn sub_assign(&mut self, other: Duration) {
85 *self = *self - other;
86 }
87}
88
89impl Sub<Instant> for Instant {
90 type Output = Duration;
91
92 /// Returns the amount of time elapsed from another instant to this one,
93 /// or zero duration if that instant is later than this one.
94 fn sub(self, other: Instant) -> Duration {
95 self.duration_since(other)
96 }
97}