| 1 | //! Counter-width abstraction for histogram bucket counts. |
| 2 | //! |
| 3 | //! The [`Count`] trait abstracts over the bucket counter width. It is |
| 4 | //! implemented for `u32` and `u64`. The trait is sealed: it cannot be |
| 5 | //! implemented outside this crate. |
| 6 | //! |
| 7 | //! [`AtomicCount`] is the matching atomic-primitive trait, mapped via the |
| 8 | //! [`Count::Atomic`] associated type. It is implemented for `AtomicU32` |
| 9 | //! (paired with `u32`) and `AtomicU64` (paired with `u64`). |
| 10 | |
| 11 | use core::sync::atomic::{AtomicU32, AtomicU64, Ordering}; |
| 12 | |
| 13 | mod private { |
| 14 | pub trait Sealed {} |
| 15 | } |
| 16 | |
| 17 | /// A counter type usable for histogram bucket counts. |
| 18 | /// |
| 19 | /// Sealed: implemented only for `u32` and `u64` inside this crate. |
| 20 | pub trait Count: private::Sealed + Copy + Default + Eq + Ord + std::fmt::Debug + 'static { |
| 21 | /// The atomic-primitive counterpart used by `AtomicHistogram<Self>`. |
| 22 | type Atomic: AtomicCount<Value = Self>; |
| 23 | |
| 24 | /// The additive identity for this counter type. |
| 25 | const ZERO: Self; |
| 26 | /// The multiplicative identity (used by `increment`). |
| 27 | const ONE: Self; |
| 28 | |
| 29 | fn wrapping_add(self, other: Self) -> Self; |
| 30 | fn wrapping_sub(self, other: Self) -> Self; |
| 31 | fn checked_add(self, other: Self) -> Option<Self>; |
| 32 | fn checked_sub(self, other: Self) -> Option<Self>; |
| 33 | |
| 34 | /// Widen to `u128` for partial-sum aggregation. |
| 35 | fn as_u128(self) -> u128; |
| 36 | /// Narrow from `u64`. Returns `None` if `v` exceeds the range of `Self`. |
| 37 | fn try_from_u64(v: u64) -> Option<Self>; |
| 38 | } |
| 39 | |
| 40 | /// Atomic counterpart of a [`Count`] type. |
| 41 | /// |
| 42 | /// Sealed: implemented only for `AtomicU32` and `AtomicU64` inside this crate. |
| 43 | pub trait AtomicCount: private::Sealed { |
| 44 | type Value: Count<Atomic = Self>; |
| 45 | |
| 46 | fn new(v: Self::Value) -> Self; |
| 47 | fn load_relaxed(&self) -> Self::Value; |
| 48 | fn store_relaxed(&self, v: Self::Value); |
| 49 | fn fetch_add_relaxed(&self, v: Self::Value); |
| 50 | fn swap_relaxed(&self, v: Self::Value) -> Self::Value; |
| 51 | } |
| 52 | |
| 53 | impl private::Sealed for u32 {} |
| 54 | impl private::Sealed for u64 {} |
| 55 | impl private::Sealed for AtomicU32 {} |
| 56 | impl private::Sealed for AtomicU64 {} |
| 57 | |
| 58 | impl Count for u32 { |
| 59 | type Atomic = AtomicU32; |
| 60 | const ZERO: Self = 0; |
| 61 | const ONE: Self = 1; |
| 62 | |
| 63 | #[inline] |
| 64 | fn wrapping_add(self, other: Self) -> Self { |
| 65 | u32::wrapping_add(self, other) |
| 66 | } |
| 67 | #[inline] |
| 68 | fn wrapping_sub(self, other: Self) -> Self { |
| 69 | u32::wrapping_sub(self, other) |
| 70 | } |
| 71 | #[inline] |
| 72 | fn checked_add(self, other: Self) -> Option<Self> { |
| 73 | u32::checked_add(self, other) |
| 74 | } |
| 75 | #[inline] |
| 76 | fn checked_sub(self, other: Self) -> Option<Self> { |
| 77 | u32::checked_sub(self, other) |
| 78 | } |
| 79 | #[inline] |
| 80 | fn as_u128(self) -> u128 { |
| 81 | self as u128 |
| 82 | } |
| 83 | #[inline] |
| 84 | fn try_from_u64(v: u64) -> Option<Self> { |
| 85 | u32::try_from(v).ok() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Count for u64 { |
| 90 | type Atomic = AtomicU64; |
| 91 | const ZERO: Self = 0; |
| 92 | const ONE: Self = 1; |
| 93 | |
| 94 | #[inline] |
| 95 | fn wrapping_add(self, other: Self) -> Self { |
| 96 | u64::wrapping_add(self, other) |
| 97 | } |
| 98 | #[inline] |
| 99 | fn wrapping_sub(self, other: Self) -> Self { |
| 100 | u64::wrapping_sub(self, other) |
| 101 | } |
| 102 | #[inline] |
| 103 | fn checked_add(self, other: Self) -> Option<Self> { |
| 104 | u64::checked_add(self, other) |
| 105 | } |
| 106 | #[inline] |
| 107 | fn checked_sub(self, other: Self) -> Option<Self> { |
| 108 | u64::checked_sub(self, other) |
| 109 | } |
| 110 | #[inline] |
| 111 | fn as_u128(self) -> u128 { |
| 112 | self as u128 |
| 113 | } |
| 114 | #[inline] |
| 115 | fn try_from_u64(v: u64) -> Option<Self> { |
| 116 | Some(v) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | impl AtomicCount for AtomicU32 { |
| 121 | type Value = u32; |
| 122 | #[inline] |
| 123 | fn new(v: u32) -> Self { |
| 124 | AtomicU32::new(v) |
| 125 | } |
| 126 | #[inline] |
| 127 | fn load_relaxed(&self) -> u32 { |
| 128 | self.load(Ordering::Relaxed) |
| 129 | } |
| 130 | #[inline] |
| 131 | fn store_relaxed(&self, v: u32) { |
| 132 | self.store(v, Ordering::Relaxed) |
| 133 | } |
| 134 | #[inline] |
| 135 | fn fetch_add_relaxed(&self, v: u32) { |
| 136 | self.fetch_add(v, Ordering::Relaxed); |
| 137 | } |
| 138 | #[inline] |
| 139 | fn swap_relaxed(&self, v: u32) -> u32 { |
| 140 | self.swap(v, Ordering::Relaxed) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl AtomicCount for AtomicU64 { |
| 145 | type Value = u64; |
| 146 | #[inline] |
| 147 | fn new(v: u64) -> Self { |
| 148 | AtomicU64::new(v) |
| 149 | } |
| 150 | #[inline] |
| 151 | fn load_relaxed(&self) -> u64 { |
| 152 | self.load(Ordering::Relaxed) |
| 153 | } |
| 154 | #[inline] |
| 155 | fn store_relaxed(&self, v: u64) { |
| 156 | self.store(v, Ordering::Relaxed) |
| 157 | } |
| 158 | #[inline] |
| 159 | fn fetch_add_relaxed(&self, v: u64) { |
| 160 | self.fetch_add(v, Ordering::Relaxed); |
| 161 | } |
| 162 | #[inline] |
| 163 | fn swap_relaxed(&self, v: u64) -> u64 { |
| 164 | self.swap(v, Ordering::Relaxed) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #[cfg(test)] |
| 169 | mod tests { |
| 170 | use super::*; |
| 171 | |
| 172 | #[test] |
| 173 | fn u32_const_values() { |
| 174 | assert_eq!(<u32 as Count>::ZERO, 0u32); |
| 175 | assert_eq!(<u32 as Count>::ONE, 1u32); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn u64_const_values() { |
| 180 | assert_eq!(<u64 as Count>::ZERO, 0u64); |
| 181 | assert_eq!(<u64 as Count>::ONE, 1u64); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn u32_wrapping_arithmetic() { |
| 186 | assert_eq!(<u32 as Count>::wrapping_add(u32::MAX, 1), 0); |
| 187 | assert_eq!(<u32 as Count>::wrapping_sub(0, 1), u32::MAX); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn u32_checked_arithmetic() { |
| 192 | assert_eq!(<u32 as Count>::checked_add(u32::MAX, 1), None); |
| 193 | assert_eq!(<u32 as Count>::checked_sub(0u32, 1), None); |
| 194 | assert_eq!(<u32 as Count>::checked_add(1u32, 1), Some(2)); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn try_from_u64_narrowing() { |
| 199 | assert_eq!(<u32 as Count>::try_from_u64(42), Some(42u32)); |
| 200 | assert_eq!( |
| 201 | <u32 as Count>::try_from_u64(u32::MAX as u64), |
| 202 | Some(u32::MAX) |
| 203 | ); |
| 204 | assert_eq!(<u32 as Count>::try_from_u64(u32::MAX as u64 + 1), None); |
| 205 | assert_eq!(<u64 as Count>::try_from_u64(u64::MAX), Some(u64::MAX)); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn as_u128_widening() { |
| 210 | assert_eq!(<u32 as Count>::as_u128(u32::MAX), u32::MAX as u128); |
| 211 | assert_eq!(<u64 as Count>::as_u128(u64::MAX), u64::MAX as u128); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn atomic_u32_basic() { |
| 216 | let a = <AtomicU32 as AtomicCount>::new(0); |
| 217 | a.fetch_add_relaxed(5); |
| 218 | assert_eq!(a.load_relaxed(), 5); |
| 219 | let prev = a.swap_relaxed(10); |
| 220 | assert_eq!(prev, 5); |
| 221 | assert_eq!(a.load_relaxed(), 10); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn atomic_u64_basic() { |
| 226 | let a = <AtomicU64 as AtomicCount>::new(0); |
| 227 | a.fetch_add_relaxed(5); |
| 228 | assert_eq!(a.load_relaxed(), 5); |
| 229 | } |
| 230 | } |