s2n_quic_core/event/metrics/aggregate/
metric.rs1use super::info::Str;
5use core::time::Duration;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[non_exhaustive]
9pub enum Units {
10 None,
11 Bytes,
12 Duration,
13 Percent,
14}
15
16impl Units {
17 pub const fn as_str(&self) -> &'static Str {
18 match self {
19 Units::None => Str::new("\0"),
20 Units::Bytes => Str::new("bytes\0"),
21 Units::Duration => Str::new("duration\0"),
22 Units::Percent => Str::new("percent\0"),
23 }
24 }
25}
26
27pub trait Metric: 'static + Send + Sync + Copy + core::fmt::Debug {
28 #[inline]
29 fn is_f32(&self) -> bool {
30 false
31 }
32 fn as_f32(&self) -> f32;
33
34 #[inline]
35 fn is_f64(&self) -> bool {
36 false
37 }
38 fn as_f64(&self) -> f64;
39
40 #[inline]
41 fn is_u64(&self) -> bool {
42 false
43 }
44 fn as_u64(&self) -> u64;
45
46 #[inline]
47 fn is_duration(&self) -> bool {
48 false
49 }
50 fn as_duration(&self) -> Duration;
51}
52
53impl Metric for f32 {
54 #[inline]
55 fn as_f32(&self) -> f32 {
56 *self
57 }
58
59 #[inline]
60 fn is_f32(&self) -> bool {
61 true
62 }
63
64 #[inline]
65 fn as_f64(&self) -> f64 {
66 *self as _
67 }
68
69 #[inline]
70 fn as_u64(&self) -> u64 {
71 *self as _
72 }
73
74 #[inline]
75 fn as_duration(&self) -> Duration {
76 Duration::from_secs_f32(*self)
77 }
78}
79
80impl Metric for f64 {
81 #[inline]
82 fn as_f32(&self) -> f32 {
83 *self as _
84 }
85
86 #[inline]
87 fn as_f64(&self) -> f64 {
88 *self
89 }
90
91 #[inline]
92 fn is_f64(&self) -> bool {
93 true
94 }
95
96 #[inline]
97 fn as_u64(&self) -> u64 {
98 *self as _
99 }
100
101 #[inline]
102 fn as_duration(&self) -> Duration {
103 Duration::from_secs_f64(*self)
104 }
105}
106
107impl Metric for Duration {
108 #[inline]
109 fn as_f32(&self) -> f32 {
110 self.as_secs_f32()
111 }
112
113 #[inline]
114 fn as_f64(&self) -> f64 {
115 self.as_secs_f64()
116 }
117
118 #[inline]
119 fn as_u64(&self) -> u64 {
120 self.as_micros() as _
121 }
122
123 #[inline]
124 fn is_duration(&self) -> bool {
125 true
126 }
127
128 #[inline]
129 fn as_duration(&self) -> Duration {
130 *self
131 }
132}
133
134macro_rules! impl_metric_number {
135 ($($ty:ty),* $(,)?) => {
136 $(
137 impl Metric for $ty {
138 #[inline]
139 fn as_f32(&self) -> f32 {
140 *self as _
141 }
142
143 #[inline]
144 fn as_f64(&self) -> f64 {
145 *self as _
146 }
147
148 #[inline]
149 fn is_u64(&self) -> bool {
150 true
151 }
152
153 #[inline]
154 fn as_u64(&self) -> u64 {
155 *self as _
156 }
157
158 #[inline]
159 fn as_duration(&self) -> Duration {
160 Duration::from_micros(*self as _)
161 }
162 }
163 )*
164 }
165}
166
167impl_metric_number!(u8, u16, u32, u64, usize);