Skip to content

Commit

Permalink
*: Remove all occurrences of std::sync::atomic::AtomicU64 for unsuppo…
Browse files Browse the repository at this point in the history
…rted platforms

Signed-off-by: Chitoku <odango@chitoku.jp>
  • Loading branch information
chitoku-k committed Jan 31, 2022
1 parent af0137a commit 5bfff61
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/metrics/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use super::{MetricType, TypedMetric};
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

/// Open Metrics [`Counter`] to measure discrete events.
Expand Down Expand Up @@ -36,11 +38,18 @@ use std::sync::Arc;
/// counter.inc();
/// let _value: f64 = counter.get();
/// ```
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
pub struct Counter<N = u64, A = AtomicU64> {
value: Arc<A>,
phantom: PhantomData<N>,
}

#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
pub struct Counter<N = u32, A = AtomicU32> {
value: Arc<A>,
phantom: PhantomData<N>,
}

impl<N, A> Clone for Counter<N, A> {
fn clone(&self) -> Self {
Self {
Expand Down Expand Up @@ -96,6 +105,7 @@ pub trait Atomic<N> {
fn get(&self) -> N;
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
impl Atomic<u64> for AtomicU64 {
fn inc(&self) -> u64 {
self.inc_by(1)
Expand Down Expand Up @@ -124,6 +134,7 @@ impl Atomic<u32> for AtomicU32 {
}
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
impl Atomic<f64> for AtomicU64 {
fn inc(&self) -> f64 {
self.inc_by(1.0)
Expand Down Expand Up @@ -165,6 +176,7 @@ mod tests {
assert_eq!(1, counter.get());
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[test]
fn f64_stored_in_atomic_u64() {
fn prop(fs: Vec<f64>) {
Expand Down
9 changes: 9 additions & 0 deletions src/metrics/exemplar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use super::counter::{self, Counter};
use super::histogram::Histogram;
use owning_ref::OwningRef;
use std::collections::HashMap;
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
use std::sync::atomic::AtomicU32;
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
use std::sync::atomic::AtomicU64;
use std::sync::{Arc, RwLock, RwLockReadGuard};

Expand All @@ -26,10 +29,16 @@ pub struct Exemplar<S, V> {
/// counter_with_exemplar.inc_by(1, Some(vec![("user_id".to_string(), "42".to_string())]));
/// let _value: (u64, _) = counter_with_exemplar.get();
/// ```
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
pub struct CounterWithExemplar<S, N = u64, A = AtomicU64> {
pub(crate) inner: Arc<RwLock<CounterWithExemplarInner<S, N, A>>>,
}

#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
pub struct CounterWithExemplar<S, N = u32, A = AtomicU32> {
pub(crate) inner: Arc<RwLock<CounterWithExemplarInner<S, N, A>>>,
}

impl<S, N, A> Clone for CounterWithExemplar<S, N, A> {
fn clone(&self) -> Self {
CounterWithExemplar {
Expand Down
13 changes: 12 additions & 1 deletion src/metrics/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use super::{MetricType, TypedMetric};
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

/// Open Metrics [`Gauge`] to record current measurements.
Expand Down Expand Up @@ -36,11 +38,18 @@ use std::sync::Arc;
/// gauge.set(42.0);
/// let _value: f64 = gauge.get();
/// ```
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
pub struct Gauge<N = u64, A = AtomicU64> {
value: Arc<A>,
phantom: PhantomData<N>,
}

#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
pub struct Gauge<N = u32, A = AtomicU32> {
value: Arc<A>,
phantom: PhantomData<N>,
}

impl<N, A> Clone for Gauge<N, A> {
fn clone(&self) -> Self {
Self {
Expand Down Expand Up @@ -113,6 +122,7 @@ pub trait Atomic<N> {
fn get(&self) -> N;
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
impl Atomic<u64> for AtomicU64 {
fn inc(&self) -> u64 {
self.inc_by(1)
Expand Down Expand Up @@ -165,6 +175,7 @@ impl Atomic<u32> for AtomicU32 {
}
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
impl Atomic<f64> for AtomicU64 {
fn inc(&self) -> f64 {
self.inc_by(1.0)
Expand Down

0 comments on commit 5bfff61

Please sign in to comment.