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
use serde::ser::{Serialize, Serializer};
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct CpuDelta {
micros: u64,
}
impl From<Duration> for CpuDelta {
fn from(duration: Duration) -> Self {
Self {
micros: duration.as_micros() as u64,
}
}
}
impl CpuDelta {
pub const ZERO: Self = Self { micros: 0 };
pub fn from_nanos(nanos: u64) -> Self {
Self {
micros: nanos / 1000,
}
}
pub fn from_micros(micros: u64) -> Self {
Self { micros }
}
pub fn from_millis(millis: f64) -> Self {
Self {
micros: (millis * 1_000.0) as u64,
}
}
pub fn is_zero(&self) -> bool {
self.micros == 0
}
}
impl Serialize for CpuDelta {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.micros.serialize(serializer)
}
}