From 7d32407932c9e9d031a948da9471bc6f70116a73 Mon Sep 17 00:00:00 2001 From: Jan Teske Date: Wed, 14 Jun 2023 15:05:12 +0200 Subject: [PATCH] ore/metrics: implement `Borrow` for drop wrapper types This commit adds `Borrow` implementations to the wrapper types `DeleteOnDropHistogram`, `DeleteOnDropCounter`, and `DeleteOnDropGauge`. This makes it possible to write code that is generic over both the prometheus type and the wrapped type. The existing `Deref` impl does not work for this. --- src/ore/src/metrics/delete_on_drop.rs | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/ore/src/metrics/delete_on_drop.rs b/src/ore/src/metrics/delete_on_drop.rs index 6f7fb99cdbe7d..fd2cf9f92aa09 100644 --- a/src/ore/src/metrics/delete_on_drop.rs +++ b/src/ore/src/metrics/delete_on_drop.rs @@ -27,6 +27,7 @@ //! * When using owned data (an extension over what Prometheus allows, which only lets you use //! references to refer to labels), the created metric is also allowed to live for `'static`. +use std::borrow::Borrow; use std::collections::BTreeMap; use std::marker::PhantomData; use std::ops::Deref; @@ -149,6 +150,10 @@ impl<'a> PromLabelsExt<'a> for BTreeMap<&'a str, &'a str> { /// /// It adds a method to create a concrete metric from the vector that gets removed from the vector /// when the concrete metric is dropped. +/// +/// NOTE: This type implements [`Borrow`], which imposes some constraints on implementers. To +/// ensure these constraints, do *not* implement any of the `Eq`, `Ord`, or `Hash` traits on this. +/// type. #[derive(Debug)] pub struct DeleteOnDropHistogram<'a, L> where @@ -185,6 +190,15 @@ where } } +impl<'a, L> Borrow for DeleteOnDropHistogram<'a, L> +where + L: PromLabelsExt<'a>, +{ + fn borrow(&self) -> &Histogram { + &self.inner + } +} + impl<'a, L> Drop for DeleteOnDropHistogram<'a, L> where L: PromLabelsExt<'a>, @@ -200,6 +214,10 @@ where /// /// It adds a method to create a concrete metric from the vector that gets removed from the vector /// when the concrete metric is dropped. +/// +/// NOTE: This type implements [`Borrow`], which imposes some constraints on implementers. To +/// ensure these constraints, do *not* implement any of the `Eq`, `Ord`, or `Hash` traits on this. +/// type. #[derive(Debug)] pub struct DeleteOnDropCounter<'a, P, L> where @@ -239,6 +257,16 @@ where } } +impl<'a, P, L> Borrow> for DeleteOnDropCounter<'a, P, L> +where + P: Atomic, + L: PromLabelsExt<'a>, +{ + fn borrow(&self) -> &GenericCounter

{ + &self.inner + } +} + impl<'a, P, L> Drop for DeleteOnDropCounter<'a, P, L> where P: Atomic, @@ -301,6 +329,10 @@ impl HistogramVecExt for HistogramVec { } /// A [`GenericGauge`] wrapper that deletes its labels from the vec when it is dropped +/// +/// NOTE: This type implements [`Borrow`], which imposes some constraints on implementers. To +/// ensure these constraints, do *not* implement any of the `Eq`, `Ord`, or `Hash` traits on this. +/// type. #[derive(Debug)] pub struct DeleteOnDropGauge<'a, P, L> where @@ -340,6 +372,16 @@ where } } +impl<'a, P, L> Borrow> for DeleteOnDropGauge<'a, P, L> +where + P: Atomic, + L: PromLabelsExt<'a>, +{ + fn borrow(&self) -> &GenericGauge

{ + &self.inner + } +} + impl<'a, P, L> Drop for DeleteOnDropGauge<'a, P, L> where P: Atomic,