turbo_trace_server/
timestamp.rs

1use std::{
2    fmt::{Debug, Display, Formatter},
3    iter::Sum,
4    ops::{Add, AddAssign, Deref, Div, DivAssign, Mul, MulAssign, Sub, SubAssign},
5};
6
7use serde::{Deserialize, Serialize};
8
9const DUR_VALUE_MICROSECOND: u64 = 100;
10
11#[derive(Clone, Copy, Default, Serialize, Deserialize)]
12pub struct Timestamp(u64);
13
14impl Timestamp {
15    pub const MAX: Self = Self(u64::MAX);
16    pub const ZERO: Self = Self(0);
17}
18
19impl Timestamp {
20    pub fn from_micros(micros: u64) -> Self {
21        Self(micros * DUR_VALUE_MICROSECOND)
22    }
23
24    pub fn is_zero(&self) -> bool {
25        self.0 == 0
26    }
27
28    pub fn from_value(value: u64) -> Self {
29        Self(value)
30    }
31
32    pub fn saturating_sub(self, rhs: Self) -> Self {
33        Self(self.0.saturating_sub(rhs.0))
34    }
35}
36
37impl Debug for Timestamp {
38    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
39        write!(f, "{:.2}μs", self.0 as f64 / DUR_VALUE_MICROSECOND as f64)
40    }
41}
42
43impl Display for Timestamp {
44    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
45        write!(f, "{:.2}μs", self.0 as f64 / DUR_VALUE_MICROSECOND as f64)
46    }
47}
48
49impl Add for Timestamp {
50    type Output = Self;
51
52    fn add(self, rhs: Self) -> Self {
53        Self(self.0 + rhs.0)
54    }
55}
56
57impl AddAssign for Timestamp {
58    fn add_assign(&mut self, rhs: Self) {
59        self.0 += rhs.0;
60    }
61}
62
63impl Sub for Timestamp {
64    type Output = Self;
65
66    fn sub(self, rhs: Self) -> Self {
67        Self(self.0 - rhs.0)
68    }
69}
70
71impl SubAssign for Timestamp {
72    fn sub_assign(&mut self, rhs: Self) {
73        self.0 -= rhs.0;
74    }
75}
76
77impl Div<u64> for Timestamp {
78    type Output = Self;
79
80    fn div(self, rhs: u64) -> Self {
81        Self(self.0 / rhs)
82    }
83}
84
85impl DivAssign<u64> for Timestamp {
86    fn div_assign(&mut self, rhs: u64) {
87        self.0 /= rhs;
88    }
89}
90
91impl Div for Timestamp {
92    type Output = u64;
93
94    fn div(self, rhs: Self) -> u64 {
95        self.0 / rhs.0
96    }
97}
98
99impl Mul<u64> for Timestamp {
100    type Output = Self;
101
102    fn mul(self, rhs: u64) -> Self {
103        Self(self.0 * rhs)
104    }
105}
106
107impl MulAssign<u64> for Timestamp {
108    fn mul_assign(&mut self, rhs: u64) {
109        self.0 *= rhs;
110    }
111}
112
113impl Sum for Timestamp {
114    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
115        iter.fold(Timestamp(0), |a, b| a + b)
116    }
117}
118
119impl PartialEq for Timestamp {
120    fn eq(&self, other: &Self) -> bool {
121        self.0 == other.0
122    }
123}
124
125impl Eq for Timestamp {}
126
127impl Ord for Timestamp {
128    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
129        self.0.cmp(&other.0)
130    }
131}
132
133impl PartialOrd for Timestamp {
134    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
135        Some(self.cmp(other))
136    }
137}
138
139impl Deref for Timestamp {
140    type Target = u64;
141
142    fn deref(&self) -> &Self::Target {
143        &self.0
144    }
145}