From d57646f3036d7ba1dfd45e5bbc57c064f18133c8 Mon Sep 17 00:00:00 2001 From: tyranron Date: Wed, 14 Dec 2022 19:31:52 +0100 Subject: [PATCH 1/2] Allow into/from `Arc` conversions Signed-off-by: tyranron --- src/counter.rs | 31 +++++++++++++++++++++++++++++++ src/gauge.rs | 31 +++++++++++++++++++++++++++++++ src/histogram.rs | 13 +++++++++++++ 3 files changed, 75 insertions(+) diff --git a/src/counter.rs b/src/counter.rs index 239e2982..da5db866 100644 --- a/src/counter.rs +++ b/src/counter.rs @@ -16,6 +16,7 @@ use crate::vec::{MetricVec, MetricVecBuilder}; /// The underlying implementation for [`Counter`] and [`IntCounter`]. #[derive(Debug)] +#[repr(transparent)] pub struct GenericCounter { v: Arc>, } @@ -85,6 +86,36 @@ impl GenericCounter

{ pub fn local(&self) -> GenericLocalCounter

{ 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> { + 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>>) -> Self { + Self { v: v.into() } + } } impl Collector for GenericCounter

{ diff --git a/src/gauge.rs b/src/gauge.rs index 83eba2b1..724b6b7f 100644 --- a/src/gauge.rs +++ b/src/gauge.rs @@ -14,6 +14,7 @@ use crate::vec::{MetricVec, MetricVecBuilder}; /// The underlying implementation for [`Gauge`] and [`IntGauge`]. #[derive(Debug)] +#[repr(transparent)] pub struct GenericGauge { v: Arc>, } @@ -88,6 +89,36 @@ impl GenericGauge

{ 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> { + 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>>) -> Self { + Self { v: v.into() } + } } impl Collector for GenericGauge

{ diff --git a/src/histogram.rs b/src/histogram.rs index 78480541..8f86a7a9 100644 --- a/src/histogram.rs +++ b/src/histogram.rs @@ -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, } @@ -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 { + this.core + } + + /// Wraps the provided [`HistogramCore`] into a [`Histogram`]. + #[inline] + pub fn from_arc_core(core: impl Into>) -> Self { + Self { core: core.into() } + } } impl Metric for Histogram { From 1b30ba31af66ada6db0d4a54fad918f73414db69 Mon Sep 17 00:00:00 2001 From: tyranron Date: Wed, 14 Dec 2022 20:10:11 +0100 Subject: [PATCH 2/2] Mention in CHANGELOG Signed-off-by: tyranron --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f2dd610..a95d7ca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)