hdrhistogram/core/
counter.rs

1use num_traits as num;
2use std::fmt;
3
4/// This trait represents the operations a histogram must be able to perform on the underlying
5/// counter type. The `ToPrimitive` trait is needed to perform floating point operations on the
6/// counts (usually for quantiles). The `FromPrimitive` to convert back into an integer count.
7/// Partial ordering is used for threshholding, also usually in the context of quantiles.
8pub trait Counter:
9    num::Num
10    + num::ToPrimitive
11    + num::FromPrimitive
12    + num::Saturating
13    + num::CheckedSub
14    + num::CheckedAdd
15    + Copy
16    + PartialOrd<Self>
17    + fmt::Debug
18{
19    /// Counter as a f64.
20    fn as_f64(&self) -> f64;
21    /// Counter as a u64.
22    fn as_u64(&self) -> u64;
23}
24
25impl Counter for u8 {
26    #[inline]
27    fn as_f64(&self) -> f64 {
28        f64::from(*self)
29    }
30    #[inline]
31    fn as_u64(&self) -> u64 {
32        u64::from(*self)
33    }
34}
35
36impl Counter for u16 {
37    #[inline]
38    fn as_f64(&self) -> f64 {
39        f64::from(*self)
40    }
41    #[inline]
42    fn as_u64(&self) -> u64 {
43        u64::from(*self)
44    }
45}
46
47impl Counter for u32 {
48    #[inline]
49    fn as_f64(&self) -> f64 {
50        f64::from(*self)
51    }
52    #[inline]
53    fn as_u64(&self) -> u64 {
54        u64::from(*self)
55    }
56}
57
58impl Counter for u64 {
59    #[inline]
60    fn as_f64(&self) -> f64 {
61        *self as f64
62    }
63    #[inline]
64    fn as_u64(&self) -> u64 {
65        *self
66    }
67}