1//! Atomic types used for metrics.
2//!
3//! As the most commonly used types for metrics storage are atomic integers, implementations of
4//! `CounterFn` and `GaugeFn` must be provided in this crate due to Rust's "orphan rules", which
5//! disallow a crate from implementing a foreign trait on a foreign type.
6//!
7//! Further, we always require an atomic integer of a certain size regardless of whether the
8//! standard library exposes an atomic integer of that size for the target architecture.
9//!
10//! As such, the atomic types that we provide handle implementations for are publicly re-exporter
11//! here for downstream crates to utilize.
1213use std::sync::atomic::Ordering;
1415#[cfg(target_pointer_width = "32")]
16pub use portable_atomic::AtomicU64;
17#[cfg(not(target_pointer_width = "32"))]
18pub use std::sync::atomic::AtomicU64;
1920use super::{CounterFn, GaugeFn};
2122impl CounterFn for AtomicU64 {
23fn increment(&self, value: u64) {
24let _ = self.fetch_add(value, Ordering::Release);
25 }
2627fn absolute(&self, value: u64) {
28let _ = self.fetch_max(value, Ordering::AcqRel);
29 }
30}
3132impl GaugeFn for AtomicU64 {
33fn increment(&self, value: f64) {
34loop {
35let result = self.fetch_update(Ordering::AcqRel, Ordering::Relaxed, |curr| {
36let input = f64::from_bits(curr);
37let output = input + value;
38Some(output.to_bits())
39 });
4041if result.is_ok() {
42break;
43 }
44 }
45 }
4647fn decrement(&self, value: f64) {
48loop {
49let result = self.fetch_update(Ordering::AcqRel, Ordering::Relaxed, |curr| {
50let input = f64::from_bits(curr);
51let output = input - value;
52Some(output.to_bits())
53 });
5455if result.is_ok() {
56break;
57 }
58 }
59 }
6061fn set(&self, value: f64) {
62let _ = self.swap(value.to_bits(), Ordering::AcqRel);
63 }
64}