1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*<LICENSE>
    This file is part of Memthol.

    Copyright (C) 2020 OCamlPro.

    Memthol is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Memthol is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Memthol.  If not, see <https://www.gnu.org/licenses/>.
*/

//! A thin wrapper around a duration.

prelude! { time::* }

/// Wrapper around a duration.
///
/// This type represents a point in time **relative to** the start time of the run of the program
/// being profiled, which is a [`Date`][time::Date] (an absolute point in time).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SinceStart {
    /// Actual duration.
    duration: Duration,
}

impl SinceStart {
    /// A duration of 0 nanoseconds.
    pub fn zero() -> Self {
        SinceStart {
            duration: std::time::Duration::new(0, 0),
        }
    }
    /// A duration of 1 second.
    pub fn one_sec() -> Self {
        SinceStart {
            duration: std::time::Duration::new(1, 0),
        }
    }

    /// Constructor from an amount of seconds.
    pub fn from_secs(secs: u64) -> Self {
        Self::from_nano_timestamp(secs, 0)
    }

    /// Constructor from a timestamp in nanos seconds.
    pub fn from_nano_timestamp(secs: u64, nanos: u32) -> Self {
        Self {
            duration: Duration::new(secs, nanos),
        }
    }

    /// True if the duration is zero.
    pub fn is_zero(&self) -> bool {
        self.duration.subsec_nanos() == 0 && self.duration.as_secs() == 0
    }

    /// Turns itself in a lifetime.
    pub fn to_lifetime(self) -> Lifetime {
        Lifetime::from(self.duration)
    }
}

impl DurationExt for SinceStart {
    fn as_duration(&self) -> &Duration {
        &self.duration
    }
}

implement! {
    impl SinceStart {
        Display {
            |&self, fmt| {
                self.display_micros().fmt(fmt)
            }
        }

        From {
            from Duration => |duration| Self { duration }
        }
        Into {
            to Duration => |self| self.duration
        }

        Deref {
            to Duration => |&self| &self.duration
        }
    }
}

impl ops::Sub for SinceStart {
    type Output = Self;
    fn sub(mut self, other: Self) -> Self {
        self.duration = self.duration - other.duration;
        self
    }
}
impl<'a> ops::Sub<SinceStart> for &'a SinceStart {
    type Output = SinceStart;
    fn sub(self, mut other: SinceStart) -> SinceStart {
        other.duration = self.duration - other.duration;
        other
    }
}
impl<'a> ops::Sub<&'a SinceStart> for SinceStart {
    type Output = SinceStart;
    fn sub(mut self, other: &'a SinceStart) -> SinceStart {
        self.duration = self.duration - other.duration;
        self
    }
}
impl<'a, 'b> ops::Sub<&'a SinceStart> for &'b SinceStart {
    type Output = SinceStart;
    fn sub(self, other: &'a SinceStart) -> SinceStart {
        SinceStart {
            duration: self.duration - other.duration,
        }
    }
}

impl ops::Add for SinceStart {
    type Output = Self;
    fn add(mut self, other: Self) -> Self {
        self.duration = self.duration + other.duration;
        self
    }
}
impl<'a> ops::Add<SinceStart> for &'a SinceStart {
    type Output = SinceStart;
    fn add(self, mut other: SinceStart) -> SinceStart {
        other.duration = self.duration + other.duration;
        other
    }
}
impl<'a> ops::Add<&'a SinceStart> for SinceStart {
    type Output = SinceStart;
    fn add(mut self, other: &'a SinceStart) -> SinceStart {
        self.duration = self.duration + other.duration;
        self
    }
}
impl<'a, 'b> ops::Add<&'a SinceStart> for &'b SinceStart {
    type Output = SinceStart;
    fn add(self, other: &'a SinceStart) -> SinceStart {
        SinceStart {
            duration: self.duration + other.duration,
        }
    }
}

impl ops::Div<u32> for SinceStart {
    type Output = Self;
    fn div(mut self, ratio: u32) -> Self {
        self.duration = self.duration / ratio;
        self
    }
}
impl<'a> ops::Div<u32> for &'a SinceStart {
    type Output = SinceStart;
    fn div(self, ratio: u32) -> SinceStart {
        let mut slf = *self;
        slf.duration = self.duration / ratio;
        slf
    }
}