From b5c33b80d8bb30398e52b73585a0b254150fd906 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 6 Jul 2023 13:59:20 +0100 Subject: [PATCH] Simplify lifetimes --- src/collector.rs | 2 +- src/encoding.rs | 66 ++++++++++++++++++------------------- src/encoding/protobuf.rs | 54 +++++++++++++++---------------- src/encoding/text.rs | 70 +++++++++++++++++++--------------------- src/registry.rs | 2 +- 5 files changed, 94 insertions(+), 100 deletions(-) diff --git a/src/collector.rs b/src/collector.rs index 44003845..eb5bdeb0 100644 --- a/src/collector.rs +++ b/src/collector.rs @@ -22,7 +22,7 @@ use crate::encoding::DescriptorEncoder; /// struct MyCollector {} /// /// impl Collector for MyCollector { -/// fn encode<'a>(&'a self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { +/// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { /// let counter = ConstCounter::new(42); /// let metric_encoder = encoder.encode_descriptor( /// "my_counter", diff --git a/src/encoding.rs b/src/encoding.rs index 143c4e29..a8ad8615 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -40,7 +40,7 @@ macro_rules! for_both { pub trait EncodeMetric { /// Encode the given instance in the OpenMetrics text encoding. // TODO: Lifetimes on MetricEncoder needed? - fn encode(&self, encoder: MetricEncoder<'_, '_, '_, '_>) -> Result<(), std::fmt::Error>; + fn encode(&self, encoder: MetricEncoder) -> Result<(), std::fmt::Error>; /// The OpenMetrics metric type of the instance. // One can not use [`TypedMetric`] directly, as associated constants are not @@ -60,87 +60,87 @@ impl EncodeMetric for Box { /// Encoder for a Metric Descriptor. #[derive(Debug)] -pub struct DescriptorEncoder<'a, 'b>(DescriptorEncoderInner<'a, 'b>); +pub struct DescriptorEncoder<'a>(DescriptorEncoderInner<'a>); #[derive(Debug)] -enum DescriptorEncoderInner<'a, 'b> { - Text(text::DescriptorEncoder<'a, 'b>), +enum DescriptorEncoderInner<'a> { + Text(text::DescriptorEncoder<'a>), #[cfg(feature = "protobuf")] - Protobuf(protobuf::DescriptorEncoder<'a, 'b>), + Protobuf(protobuf::DescriptorEncoder<'a>), } -impl<'a, 'b> From> for DescriptorEncoder<'a, 'b> { - fn from(e: text::DescriptorEncoder<'a, 'b>) -> Self { +impl<'a> From> for DescriptorEncoder<'a> { + fn from(e: text::DescriptorEncoder<'a>) -> Self { Self(DescriptorEncoderInner::Text(e)) } } #[cfg(feature = "protobuf")] -impl<'a, 'b> From> for DescriptorEncoder<'a, 'b> { - fn from(e: protobuf::DescriptorEncoder<'a, 'b>) -> Self { +impl<'a> From> for DescriptorEncoder<'a> { + fn from(e: protobuf::DescriptorEncoder<'a>) -> Self { Self(DescriptorEncoderInner::Protobuf(e)) } } -impl<'a, 'b> DescriptorEncoder<'a, 'b> { - pub(crate) fn with_prefix_and_labels<'c, 'd>( - &'c mut self, - prefix: Option<&'d Prefix>, - labels: &'d [(Cow<'static, str>, Cow<'static, str>)], +impl DescriptorEncoder<'_> { + pub(crate) fn with_prefix_and_labels<'s>( + &'s mut self, + prefix: Option<&'s Prefix>, + labels: &'s [(Cow<'static, str>, Cow<'static, str>)], // TODO: result needed? - ) -> DescriptorEncoder<'c, 'd> { + ) -> DescriptorEncoder<'s> { for_both_mut!( self, DescriptorEncoderInner, e, - e.with_prefix_and_labels(prefix, labels) + e.with_prefix_and_labels(prefix, labels).into() ) } /// Encode a descriptor. - pub fn encode_descriptor<'c, 'd, 'e>( - &'c mut self, - name: &'d str, + pub fn encode_descriptor<'s>( + &'s mut self, + name: &'s str, help: &str, - unit: Option<&'d Unit>, + unit: Option<&'s Unit>, metric_type: MetricType, - ) -> Result, std::fmt::Error> { + ) -> Result, std::fmt::Error> { for_both_mut!( self, DescriptorEncoderInner, e, - e.encode_descriptor(name, help, unit, metric_type) + Ok(e.encode_descriptor(name, help, unit, metric_type)?.into()) ) } } /// Encoder for a metric. #[derive(Debug)] -pub struct MetricEncoder<'a, 'b, 'c, 'd>(MetricEncoderInner<'a, 'b, 'c, 'd>); +pub struct MetricEncoder<'a>(MetricEncoderInner<'a>); #[derive(Debug)] -enum MetricEncoderInner<'a, 'b, 'c, 'd> { - Text(text::MetricEncoder<'a, 'b, 'c, 'd>), +enum MetricEncoderInner<'a> { + Text(text::MetricEncoder<'a>), #[cfg(feature = "protobuf")] Protobuf(protobuf::MetricEncoder<'a>), } -impl<'a, 'b, 'c, 'd> From> for MetricEncoder<'a, 'b, 'c, 'd> { - fn from(e: text::MetricEncoder<'a, 'b, 'c, 'd>) -> Self { +impl<'a> From> for MetricEncoder<'a> { + fn from(e: text::MetricEncoder<'a>) -> Self { Self(MetricEncoderInner::Text(e)) } } #[cfg(feature = "protobuf")] -impl<'a, 'b, 'c, 'd> From> for MetricEncoder<'a, 'b, 'c, 'd> { +impl<'a> From> for MetricEncoder<'a> { fn from(e: protobuf::MetricEncoder<'a>) -> Self { Self(MetricEncoderInner::Protobuf(e)) } } -impl<'a, 'b, 'c, 'd> MetricEncoder<'a, 'b, 'c, 'd> { +impl MetricEncoder<'_> { /// Encode a counter. pub fn encode_counter< S: EncodeLabelSet, @@ -184,10 +184,10 @@ impl<'a, 'b, 'c, 'd> MetricEncoder<'a, 'b, 'c, 'd> { } /// Encode a metric family. - pub fn encode_family<'e, 'f, S: EncodeLabelSet>( - &'e mut self, - label_set: &'f S, - ) -> Result, std::fmt::Error> { + pub fn encode_family<'s, S: EncodeLabelSet>( + &'s mut self, + label_set: &'s S, + ) -> Result, std::fmt::Error> { for_both_mut!( self, MetricEncoderInner, diff --git a/src/encoding/protobuf.rs b/src/encoding/protobuf.rs index 00b96bfd..53b30c3a 100644 --- a/src/encoding/protobuf.rs +++ b/src/encoding/protobuf.rs @@ -63,16 +63,16 @@ impl From for openmetrics_data_model::MetricType { /// /// This is an inner type for [`super::DescriptorEncoder`]. #[derive(Debug)] -pub(crate) struct DescriptorEncoder<'a, 'b> { +pub(crate) struct DescriptorEncoder<'a> { metric_families: &'a mut Vec, - prefix: Option<&'b Prefix>, - labels: &'b [(Cow<'static, str>, Cow<'static, str>)], + prefix: Option<&'a Prefix>, + labels: &'a [(Cow<'static, str>, Cow<'static, str>)], } -impl<'a, 'b> DescriptorEncoder<'a, 'b> { - pub(crate) fn new<'c, 'd>( - metric_families: &'c mut Vec, - ) -> DescriptorEncoder<'c, 'd> { +impl DescriptorEncoder<'_> { + pub(crate) fn new( + metric_families: &mut Vec, + ) -> DescriptorEncoder { DescriptorEncoder { metric_families, prefix: Default::default(), @@ -80,26 +80,25 @@ impl<'a, 'b> DescriptorEncoder<'a, 'b> { } } - pub(crate) fn with_prefix_and_labels<'c, 'd>( - &'c mut self, - prefix: Option<&'d Prefix>, - labels: &'d [(Cow<'static, str>, Cow<'static, str>)], - ) -> super::DescriptorEncoder<'c, 'd> { + pub(crate) fn with_prefix_and_labels<'s>( + &'s mut self, + prefix: Option<&'s Prefix>, + labels: &'s [(Cow<'static, str>, Cow<'static, str>)], + ) -> DescriptorEncoder<'s> { DescriptorEncoder { prefix, labels, metric_families: self.metric_families, } - .into() } - pub fn encode_descriptor<'c, 'd, 'e>( - &'c mut self, - name: &'d str, + pub fn encode_descriptor<'s>( + &'s mut self, + name: &str, help: &str, - unit: Option<&'d Unit>, + unit: Option<&Unit>, metric_type: MetricType, - ) -> Result, std::fmt::Error> { + ) -> Result, std::fmt::Error> { let family = openmetrics_data_model::MetricFamily { name: { match self.prefix { @@ -127,7 +126,8 @@ impl<'a, 'b> DescriptorEncoder<'a, 'b> { .into(), )?; self.metric_families.push(family); - let encoder = MetricEncoder { + + Ok(MetricEncoder { family: &mut self .metric_families .last_mut() @@ -135,9 +135,7 @@ impl<'a, 'b> DescriptorEncoder<'a, 'b> { .metrics, metric_type, labels, - }; - - Ok(encoder.into()) + }) } } @@ -145,16 +143,16 @@ impl<'a, 'b> DescriptorEncoder<'a, 'b> { /// /// This is an inner type for [`super::MetricEncoder`]. #[derive(Debug)] -pub(crate) struct MetricEncoder<'a> { +pub(crate) struct MetricEncoder<'f> { /// OpenMetrics metric type of the metric. metric_type: MetricType, /// Vector of OpenMetrics metrics to which encoded metrics are added. - family: &'a mut Vec, + family: &'f mut Vec, /// Labels to be added to each metric. labels: Vec, } -impl<'a> MetricEncoder<'a> { +impl MetricEncoder<'_> { pub fn encode_counter< S: EncodeLabelSet, CounterValue: EncodeCounterValue, @@ -231,10 +229,10 @@ impl<'a> MetricEncoder<'a> { Ok(()) } - pub fn encode_family<'b, S: EncodeLabelSet>( - &'b mut self, + pub fn encode_family( + &mut self, label_set: &S, - ) -> Result, std::fmt::Error> { + ) -> Result { let mut labels = self.labels.clone(); label_set.encode( LabelSetEncoder { diff --git a/src/encoding/text.rs b/src/encoding/text.rs index 94806c6b..d456c1af 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -44,47 +44,46 @@ where Ok(()) } -pub(crate) struct DescriptorEncoder<'a, 'b> { +pub(crate) struct DescriptorEncoder<'a> { writer: &'a mut dyn Write, - prefix: Option<&'b Prefix>, - labels: &'b [(Cow<'static, str>, Cow<'static, str>)], + prefix: Option<&'a Prefix>, + labels: &'a [(Cow<'static, str>, Cow<'static, str>)], } -impl<'a, 'b> std::fmt::Debug for DescriptorEncoder<'a, 'b> { +impl<'a> std::fmt::Debug for DescriptorEncoder<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("DescriptorEncoder").finish() } } -impl<'a, 'b> DescriptorEncoder<'a, 'b> { - pub(crate) fn new(writer: &'a mut dyn Write) -> Self { - Self { +impl DescriptorEncoder<'_> { + pub(crate) fn new(writer: &mut dyn Write) -> DescriptorEncoder { + DescriptorEncoder { writer, prefix: Default::default(), labels: Default::default(), } } - pub(crate) fn with_prefix_and_labels<'c, 'd>( - &'c mut self, - prefix: Option<&'d Prefix>, - labels: &'d [(Cow<'static, str>, Cow<'static, str>)], - ) -> super::DescriptorEncoder<'c, 'd> { + pub(crate) fn with_prefix_and_labels<'s>( + &'s mut self, + prefix: Option<&'s Prefix>, + labels: &'s [(Cow<'static, str>, Cow<'static, str>)], + ) -> DescriptorEncoder<'s> { DescriptorEncoder { prefix, labels, writer: self.writer, } - .into() } - pub fn encode_descriptor<'c, 'd, 'e>( - &'c mut self, - name: &'d str, + pub fn encode_descriptor<'s>( + &'s mut self, + name: &'s str, help: &str, - unit: Option<&'d Unit>, + unit: Option<&'s Unit>, metric_type: MetricType, - ) -> Result, std::fmt::Error> { + ) -> Result, std::fmt::Error> { self.writer.write_str("# HELP ")?; if let Some(prefix) = self.prefix { self.writer.write_str(prefix.as_str())?; @@ -127,17 +126,14 @@ impl<'a, 'b> DescriptorEncoder<'a, 'b> { self.writer.write_str("\n")?; } - let encoder = MetricEncoder { + Ok(MetricEncoder { writer: self.writer, prefix: self.prefix, name, unit, const_labels: self.labels, family_labels: None, - } - .into(); - - Ok(encoder) + }) } } @@ -149,16 +145,16 @@ impl<'a, 'b> DescriptorEncoder<'a, 'b> { // `Registry`. Trait objects can not use type parameters. // // TODO: Alternative solutions to the above are very much appreciated. -pub(crate) struct MetricEncoder<'a, 'b, 'c, 'd> { +pub(crate) struct MetricEncoder<'a> { writer: &'a mut dyn Write, - prefix: Option<&'c Prefix>, - name: &'b str, - unit: Option<&'b Unit>, - const_labels: &'c [(Cow<'static, str>, Cow<'static, str>)], - family_labels: Option<&'d dyn super::EncodeLabelSet>, + prefix: Option<&'a Prefix>, + name: &'a str, + unit: Option<&'a Unit>, + const_labels: &'a [(Cow<'static, str>, Cow<'static, str>)], + family_labels: Option<&'a dyn super::EncodeLabelSet>, } -impl<'a, 'b, 'c, 'd> std::fmt::Debug for MetricEncoder<'a, 'b, 'c, 'd> { +impl<'a> std::fmt::Debug for MetricEncoder<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut labels = String::new(); if let Some(l) = self.family_labels { @@ -175,7 +171,7 @@ impl<'a, 'b, 'c, 'd> std::fmt::Debug for MetricEncoder<'a, 'b, 'c, 'd> { } } -impl<'a, 'b, 'c, 'd> MetricEncoder<'a, 'b, 'c, 'd> { +impl<'a> MetricEncoder<'a> { pub fn encode_counter< S: EncodeLabelSet, CounterValue: super::EncodeCounterValue, @@ -244,10 +240,10 @@ impl<'a, 'b, 'c, 'd> MetricEncoder<'a, 'b, 'c, 'd> { /// Encode a set of labels. Used by wrapper metric types like /// [`Family`](crate::metrics::family::Family). - pub fn encode_family<'e, 'f, S: EncodeLabelSet>( - &'e mut self, - label_set: &'f S, - ) -> Result, std::fmt::Error> { + pub fn encode_family<'s, S: EncodeLabelSet>( + &'s mut self, + label_set: &'s S, + ) -> Result, std::fmt::Error> { debug_assert!(self.family_labels.is_none()); Ok(MetricEncoder { @@ -854,8 +850,8 @@ mod tests { } impl crate::collector::Collector for Collector { - fn encode<'a>( - &'a self, + fn encode( + &self, mut encoder: crate::encoding::DescriptorEncoder, ) -> Result<(), std::fmt::Error> { let counter = crate::metrics::counter::ConstCounter::new(42); diff --git a/src/registry.rs b/src/registry.rs index 650ba44d..eb125b99 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -162,7 +162,7 @@ impl Registry { /// struct MyCollector {} /// /// impl Collector for MyCollector { - /// fn encode<'a>(&'a self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { + /// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { /// let counter = ConstCounter::new(42); /// let metric_encoder = encoder.encode_descriptor( /// "my_counter",