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
// Copyright 2022 Twitter, Inc.
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICEN

use crate::*;
use core::hash::Hash;

/// A container for time types that stores seconds.
#[repr(transparent)]
pub struct Seconds<T> {
    pub(crate) inner: T,
}

unit!(Seconds<u32>);
atomic!(Seconds<AtomicU32>, u32);
atomic_arithmetic!(Seconds<AtomicU32>, Seconds<u32>);

impl<T> Eq for Seconds<T> where T: Eq {}

impl<T> PartialEq for Seconds<T>
where
    T: PartialEq,
{
    fn eq(&self, rhs: &Self) -> bool {
        self.inner.eq(&rhs.inner)
    }
}

impl<T> Ord for Seconds<T>
where
    T: Ord,
{
    fn cmp(&self, rhs: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&rhs.inner)
    }
}

impl<T> PartialOrd for Seconds<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> {
        self.inner.partial_cmp(&rhs.inner)
    }
}

impl<T> Hash for Seconds<T>
where
    T: Hash,
{
    fn hash<H>(&self, h: &mut H)
    where
        H: std::hash::Hasher,
    {
        self.inner.hash(h)
    }
}

impl<T> Clone for Seconds<T>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<T> Copy for Seconds<T> where T: Copy {}

/// A container for time types that stores nanoseconds.
#[repr(transparent)]
pub struct Nanoseconds<T> {
    pub(crate) inner: T,
}

unit!(Nanoseconds<u64>);
atomic!(Nanoseconds<AtomicU64>, u64);
atomic_arithmetic!(Nanoseconds<AtomicU64>, Nanoseconds<u64>);

impl<T> Eq for Nanoseconds<T> where T: Eq {}

impl<T> PartialEq for Nanoseconds<T>
where
    T: PartialEq,
{
    fn eq(&self, rhs: &Self) -> bool {
        self.inner.eq(&rhs.inner)
    }
}

impl<T> Ord for Nanoseconds<T>
where
    T: Ord,
{
    fn cmp(&self, rhs: &Self) -> std::cmp::Ordering {
        self.inner.cmp(&rhs.inner)
    }
}

impl<T> PartialOrd for Nanoseconds<T>
where
    T: PartialOrd,
{
    fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> {
        self.inner.partial_cmp(&rhs.inner)
    }
}

impl<T> Hash for Nanoseconds<T>
where
    T: Hash,
{
    fn hash<H>(&self, h: &mut H)
    where
        H: std::hash::Hasher,
    {
        self.inner.hash(h)
    }
}

impl<T> Clone for Nanoseconds<T>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<T> Copy for Nanoseconds<T> where T: Copy {}

impl From<libc::timespec> for Seconds<u32> {
    fn from(ts: libc::timespec) -> Self {
        Self {
            inner: ts.tv_sec as u32,
        }
    }
}

impl From<libc::timespec> for Seconds<AtomicU32> {
    fn from(ts: libc::timespec) -> Self {
        Self {
            inner: AtomicU32::new(ts.tv_sec as u32),
        }
    }
}

impl From<libc::timespec> for Nanoseconds<u64> {
    fn from(ts: libc::timespec) -> Self {
        Self {
            inner: ts.tv_sec as u64 * NANOS_PER_SEC + ts.tv_nsec as u64,
        }
    }
}

impl From<libc::timespec> for Nanoseconds<AtomicU64> {
    fn from(ts: libc::timespec) -> Self {
        Self {
            inner: AtomicU64::new(ts.tv_sec as u64 * NANOS_PER_SEC + ts.tv_nsec as u64),
        }
    }
}