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
use std::ops::*;

use super::precision::*;

/// A timestamp. Note that this is an opaque structure.
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Timestamp(pub(crate) u64);

/// The difference between two timestamps.
#[derive(Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd)]
pub struct Elapsed(u64);

impl Sub for Timestamp {
    type Output = Elapsed;

    #[inline]
    fn sub(self, ts: Timestamp) -> Self::Output {
        if self.0 >= ts.0 {
            Elapsed(self.0 - ts.0)
        } else {
            Elapsed(self.0 + (!ts.0) + 1)
        }
    }
}

impl Add for Elapsed {
    type Output = Elapsed;

    #[inline]
    fn add(self, other: Elapsed) -> Self::Output {
        Elapsed(self.0 + other.0)
    }
}

impl AddAssign for Elapsed {
    #[inline]
    fn add_assign(&mut self, other: Elapsed) {
        self.0 += other.0;
    }
}

impl Elapsed {
    /// Returns a nul duration
    #[inline]
    pub fn new() -> Self {
        Elapsed::default()
    }

    /// Builds a `Duration` from a number of ticks
    #[inline]
    pub fn from_ticks(ticks: u64) -> Self {
        Elapsed(ticks)
    }

    /// Returns the number of ticks for the given duration
    #[inline]
    pub fn ticks(&self) -> u64 {
        self.0
    }

    /// Returns the duration as a number of seconds
    ///
    /// # Panics
    ///
    /// Panics if the given `Precision` was not configured to measure wall time.
    #[inline]
    pub fn as_secs(&self, precision: &Precision) -> u64 {
        self.0
            / precision
                .frequency
                .expect("`Precision` must have been configured to measure wall time")
    }

    /// Returns the duration as a number of seconds (floating-point)
    ///
    /// # Panics
    ///
    /// Panics if the given `Precision` was not configured to measure wall time.
    #[inline]
    pub fn as_secs_f64(&self, precision: &Precision) -> f64 {
        self.0 as f64
            / precision
                .frequency
                .expect("`Precision` must have been configured to measure wall time")
                as f64
    }

    /// Returns the duration as milliseconds
    ///
    /// # Panics
    ///
    /// Panics if the given `Precision` was not configured to measure wall time.
    #[inline]
    pub fn as_millis(&self, precision: &Precision) -> u64 {
        self.0 * 1_000
            / precision
                .frequency
                .expect("`Precision` must have been configured to measure wall time")
    }

    /// Returns the duration as nanoseconds
    ///
    /// # Panics
    ///
    /// Panics if the given `Precision` was not configured to measure wall time.
    #[inline]
    pub fn as_ns(&self, precision: &Precision) -> u64 {
        (((self.0 as f64 * 1_000.0)
            / precision
                .frequency
                .expect("`Precision` must have been configured to measure wall time")
                as f64)
            * 1_000_000.0) as u64
    }
}