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
use num_traits::ToPrimitive;
use std::fmt;
use std::ops::Sub;
use std::time;
pub fn now_microseconds() -> Timestamp {
let t = time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap_or_else(|e| e.duration());
(t.as_secs().wrapping_mul(1_000_000) as u32)
.wrapping_add(t.subsec_micros())
.into()
}
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq)]
pub struct Timestamp(pub u32);
impl Sub for Timestamp {
type Output = Delay;
fn sub(self, other: Timestamp) -> Delay {
Delay(self.0 as i64 - other.0 as i64)
}
}
impl Default for Timestamp {
fn default() -> Timestamp {
Timestamp(0)
}
}
impl From<u32> for Timestamp {
fn from(value: u32) -> Timestamp {
Timestamp(value)
}
}
impl From<Timestamp> for u32 {
fn from(value: Timestamp) -> u32 {
value.0
}
}
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq)]
pub struct Delay(pub i64);
impl From<i64> for Delay {
fn from(value: i64) -> Delay {
Delay(value)
}
}
impl From<u32> for Delay {
fn from(value: u32) -> Delay {
Delay(value as i64)
}
}
impl From<Delay> for u32 {
fn from(value: Delay) -> u32 {
value.0 as u32
}
}
impl Default for Delay {
fn default() -> Delay {
Delay(0)
}
}
impl Sub for Delay {
type Output = Delay;
fn sub(self, other: Delay) -> Delay {
Delay(self.0 - other.0)
}
}
impl fmt::Display for Delay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Delay({})", self.0)
}
}
impl ToPrimitive for Delay {
fn to_i64(&self) -> Option<i64> {
Some(self.0)
}
fn to_u64(&self) -> Option<u64> {
Some(self.0 as u64)
}
}