Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow into/from inner Arc conversions #472

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.13.4 (unreleased)

- Add: `into_arc_value()`/`from_arc_value()` unsafe methods to `GenericCounter` and `GenericGauge` (#472)
- Add: `into_arc_core()`/`from_arc_core()` methods to `Histogram` (#472)
- Improvement: Mark `GenericCounter`, `GenericGauge` and `Histogram` as `#[repr(transparent)]` (#472)

## 0.13.3

- Bug fix: Prevent ProcessCollector underflow with CPU time counter (#465)
Expand Down
31 changes: 31 additions & 0 deletions src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::vec::{MetricVec, MetricVecBuilder};

/// The underlying implementation for [`Counter`] and [`IntCounter`].
#[derive(Debug)]
#[repr(transparent)]
pub struct GenericCounter<P: Atomic> {
v: Arc<Value<P>>,
}
Expand Down Expand Up @@ -85,6 +86,36 @@ impl<P: Atomic> GenericCounter<P> {
pub fn local(&self) -> GenericLocalCounter<P> {
GenericLocalCounter::new(self.clone())
}

/// Unwraps this counter into the inner [`Value`] representing it.
///
/// # Safety
///
/// This function is `unsafe`, because allows to bypass counter's semantics,
/// and so, the improper use of the returned [`Value`] may lead to this
/// counter behaving incorrectly.
///
/// The caller must ensure that the returned [`Value`] will be used in
/// accordance with the counter's semantics.
#[inline]
pub unsafe fn into_arc_value(this: Self) -> Arc<Value<P>> {
this.v
}

/// Wraps the provided [`Value`] into a counter.
///
/// # Safety
///
/// This function is `unsafe`, because allows to bypass counter's semantics,
/// and so, specifying the improper [`Value`] may lead to the counter
/// behaving incorrectly.
///
/// The caller must ensure that the provided [`Value`] withholds counter's
/// semantics.
#[inline]
pub unsafe fn from_arc_value(v: impl Into<Arc<Value<P>>>) -> Self {
Self { v: v.into() }
}
}

impl<P: Atomic> Collector for GenericCounter<P> {
Expand Down
31 changes: 31 additions & 0 deletions src/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::vec::{MetricVec, MetricVecBuilder};

/// The underlying implementation for [`Gauge`] and [`IntGauge`].
#[derive(Debug)]
#[repr(transparent)]
pub struct GenericGauge<P: Atomic> {
v: Arc<Value<P>>,
}
Expand Down Expand Up @@ -88,6 +89,36 @@ impl<P: Atomic> GenericGauge<P> {
pub fn get(&self) -> P::T {
self.v.get()
}

/// Unwraps this gauge into the inner [`Value`] representing it.
///
/// # Safety
///
/// This function is `unsafe`, because allows to bypass gauge's semantics,
/// and so, the improper use of the returned [`Value`] may lead to this
/// gauge behaving incorrectly.
///
/// The caller must ensure that the returned [`Value`] will be used in
/// accordance with the gauge's semantics.
#[inline]
pub unsafe fn into_arc_value(this: Self) -> Arc<Value<P>> {
this.v
}

/// Wraps the provided [`Value`] into a gauge.
///
/// # Safety
///
/// This function is `unsafe`, because allows to bypass gauge's semantics,
/// and so, specifying the improper [`Value`] may lead to the gauge behaving
/// incorrectly.
///
/// The caller must ensure that the provided [`Value`] withholds gauge's
/// semantics.
#[inline]
pub unsafe fn from_arc_value(v: impl Into<Arc<Value<P>>>) -> Self {
Self { v: v.into() }
}
}

impl<P: Atomic> Collector for GenericGauge<P> {
Expand Down
13 changes: 13 additions & 0 deletions src/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ impl Drop for HistogramTimer {
/// [1]: https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram_quantile
/// [2]: https://prometheus.io/docs/practices/histograms/
#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct Histogram {
core: Arc<HistogramCore>,
}
Expand Down Expand Up @@ -746,6 +747,18 @@ impl Histogram {
pub fn get_sample_count(&self) -> u64 {
self.core.sample_count()
}

/// Unwraps this [`Histogram`] into the inner [`HistogramCore`].
#[inline]
pub fn into_arc_core(this: Self) -> Arc<HistogramCore> {
this.core
}

/// Wraps the provided [`HistogramCore`] into a [`Histogram`].
#[inline]
pub fn from_arc_core(core: impl Into<Arc<HistogramCore>>) -> Self {
Self { core: core.into() }
}
}

impl Metric for Histogram {
Expand Down