bma_ts/
operations.rs

1use std::ops;
2use std::time::Duration;
3
4use crate::{Monotonic, Timestamp};
5
6macro_rules! impl_common_operations {
7    ($t: ty) => {
8        impl ops::Add<Duration> for $t {
9            type Output = $t;
10
11            fn add(self, rhs: Duration) -> Self::Output {
12                Self(self.0 + rhs)
13            }
14        }
15
16        impl ops::AddAssign<Duration> for $t {
17            fn add_assign(&mut self, rhs: Duration) {
18                self.0 = self.0 + rhs;
19            }
20        }
21
22        impl ops::Sub<Duration> for $t {
23            type Output = $t;
24
25            fn sub(self, rhs: Duration) -> Self::Output {
26                Self(self.0 - rhs)
27            }
28        }
29
30        impl ops::SubAssign<Duration> for $t {
31            fn sub_assign(&mut self, rhs: Duration) {
32                self.0 = self.0 - rhs;
33            }
34        }
35
36        impl ops::Sub<$t> for $t {
37            type Output = Duration;
38
39            fn sub(self, rhs: $t) -> Self::Output {
40                self.0 - rhs.0
41            }
42        }
43
44        impl Default for $t {
45            fn default() -> Self {
46                Self::now()
47            }
48        }
49
50        impl AsRef<Duration> for $t {
51            fn as_ref(&self) -> &Duration {
52                &self.0
53            }
54        }
55
56        impl AsRef<$t> for $t {
57            fn as_ref(&self) -> &$t {
58                self
59            }
60        }
61    };
62}
63
64impl_common_operations!(Timestamp);
65impl_common_operations!(Monotonic);