diff --git a/derive-encode/src/lib.rs b/derive-encode/src/lib.rs index 1858cf8d..5b3cbe32 100644 --- a/derive-encode/src/lib.rs +++ b/derive-encode/src/lib.rs @@ -72,7 +72,7 @@ pub fn derive_encode_label_set(input: TokenStream) -> TokenStream { let gen = quote! { impl prometheus_client::encoding::EncodeLabelSet for #name { - fn encode(&self, mut encoder: prometheus_client::encoding::LabelSetEncoder) -> std::result::Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut prometheus_client::encoding::LabelSetEncoder) -> std::result::Result<(), std::fmt::Error> { use prometheus_client::encoding::EncodeLabel; use prometheus_client::encoding::EncodeLabelKey; use prometheus_client::encoding::EncodeLabelValue; diff --git a/src/encoding.rs b/src/encoding.rs index a8d08ec1..663b2c74 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -203,7 +203,7 @@ impl MetricEncoder<'_> { /// An encodable label set. pub trait EncodeLabelSet { /// Encode oneself into the given encoder. - fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error>; + fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error>; } impl<'a> From> for LabelSetEncoder<'a> { @@ -331,13 +331,13 @@ impl<'a> LabelKeyEncoder<'a> { } } impl EncodeLabelSet for [T; N] { - fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> { self.as_ref().encode(encoder) } } impl EncodeLabelSet for &[T] { - fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> { if self.is_empty() { return Ok(()); } @@ -351,13 +351,13 @@ impl EncodeLabelSet for &[T] { } impl EncodeLabelSet for Vec { - fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> { self.as_slice().encode(encoder) } } impl EncodeLabelSet for NoLabelSet { - fn encode(&self, _encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + fn encode(&self, _encoder: &mut LabelSetEncoder) -> Result<(), std::fmt::Error> { Ok(()) } } diff --git a/src/encoding/protobuf.rs b/src/encoding/protobuf.rs index 095a3dba..3d8c2de7 100644 --- a/src/encoding/protobuf.rs +++ b/src/encoding/protobuf.rs @@ -119,12 +119,11 @@ impl DescriptorEncoder<'_> { ..Default::default() }; let mut labels = vec![]; - self.labels.encode( - LabelSetEncoder { - labels: &mut labels, - } - .into(), - )?; + let mut encoder = LabelSetEncoder { + labels: &mut labels, + } + .into(); + self.labels.encode(&mut encoder)?; self.metric_families.push(family); Ok(MetricEncoder { @@ -209,12 +208,11 @@ impl MetricEncoder<'_> { label_set: &impl super::EncodeLabelSet, ) -> Result<(), std::fmt::Error> { let mut info_labels = vec![]; - label_set.encode( - LabelSetEncoder { - labels: &mut info_labels, - } - .into(), - )?; + let mut encoder = LabelSetEncoder { + labels: &mut info_labels, + } + .into(); + label_set.encode(&mut encoder)?; self.family.push(openmetrics_data_model::Metric { labels: self.labels.clone(), @@ -234,12 +232,11 @@ impl MetricEncoder<'_> { label_set: &S, ) -> Result { let mut labels = self.labels.clone(); - label_set.encode( - LabelSetEncoder { - labels: &mut labels, - } - .into(), - )?; + let mut encoder = LabelSetEncoder { + labels: &mut labels, + } + .into(); + label_set.encode(&mut encoder)?; Ok(MetricEncoder { metric_type: self.metric_type, @@ -302,12 +299,11 @@ impl TryFrom<&Exemplar> .encode(ExemplarValueEncoder { value: &mut value }.into())?; let mut labels = vec![]; - exemplar.label_set.encode( - LabelSetEncoder { - labels: &mut labels, - } - .into(), - )?; + let mut encoder = LabelSetEncoder { + labels: &mut labels, + } + .into(); + exemplar.label_set.encode(&mut encoder)?; Ok(openmetrics_data_model::Exemplar { value, diff --git a/src/encoding/text.rs b/src/encoding/text.rs index 4946fe35..c18a459a 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -297,7 +297,8 @@ 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 { - l.encode(LabelSetEncoder::new(&mut labels).into())?; + let mut encoder = LabelSetEncoder::new(&mut labels).into(); + l.encode(&mut encoder)?; } f.debug_struct("Encoder") @@ -451,7 +452,7 @@ impl<'a> MetricEncoder<'a> { self.writer.write_str(" # {")?; exemplar .label_set - .encode(LabelSetEncoder::new(self.writer).into())?; + .encode(&mut LabelSetEncoder::new(self.writer).into())?; self.writer.write_str("} ")?; exemplar.value.encode( ExemplarValueEncoder { @@ -502,14 +503,15 @@ impl<'a> MetricEncoder<'a> { self.writer.write_str("{")?; self.const_labels - .encode(LabelSetEncoder::new(self.writer).into())?; + .encode(&mut LabelSetEncoder::new(self.writer).into())?; if let Some(additional_labels) = additional_labels { if !self.const_labels.is_empty() { self.writer.write_str(",")?; } - additional_labels.encode(LabelSetEncoder::new(self.writer).into())?; + let mut encoder = LabelSetEncoder::new(self.writer).into(); + additional_labels.encode(&mut encoder)?; } /// Writer impl which prepends a comma on the first call to write output to the wrapped writer @@ -539,9 +541,11 @@ impl<'a> MetricEncoder<'a> { writer: self.writer, should_prepend: true, }; - labels.encode(LabelSetEncoder::new(&mut writer).into())?; + let mut encoder = LabelSetEncoder::new(&mut writer).into(); + labels.encode(&mut encoder)?; } else { - labels.encode(LabelSetEncoder::new(self.writer).into())?; + let mut encoder = LabelSetEncoder::new(self.writer).into(); + labels.encode(&mut encoder)?; }; } @@ -936,7 +940,7 @@ mod tests { struct EmptyLabels {} impl EncodeLabelSet for EmptyLabels { - fn encode(&self, _encoder: crate::encoding::LabelSetEncoder) -> Result<(), Error> { + fn encode(&self, _encoder: &mut crate::encoding::LabelSetEncoder) -> Result<(), Error> { Ok(()) } }