fxprof_processed_profile/
cpu_delta.rs1use std::time::Duration;
2
3use serde::ser::{Serialize, Serializer};
4
5#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
13pub struct CpuDelta {
14 micros: u64,
15}
16
17impl From<Duration> for CpuDelta {
18 fn from(duration: Duration) -> Self {
19 Self {
20 micros: duration.as_micros() as u64,
21 }
22 }
23}
24
25impl CpuDelta {
26 pub const ZERO: Self = Self { micros: 0 };
28
29 pub fn from_nanos(nanos: u64) -> Self {
31 Self {
32 micros: nanos / 1000,
33 }
34 }
35
36 pub fn from_micros(micros: u64) -> Self {
38 Self { micros }
39 }
40
41 pub fn from_millis(millis: f64) -> Self {
43 Self {
44 micros: (millis * 1_000.0) as u64,
45 }
46 }
47
48 pub fn is_zero(&self) -> bool {
50 self.micros == 0
51 }
52}
53
54impl Serialize for CpuDelta {
55 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
56 self.micros.serialize(serializer)
59 }
60}