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