From c256fd263aaf8ad3ad3bdb0ac55d2a6bdf8f9da4 Mon Sep 17 00:00:00 2001
From: katelyn martin <me+cratelyn@katelyn.world>
Date: Tue, 15 Oct 2024 00:00:00 +0000
Subject: [PATCH] feat(encoding)!: `Collector` does not consume
 `DescriptorEncoder`

see #135.

this adjusts the parameter of `encode()` so that it only mutably borrows
the encoder.

Signed-off-by: katelyn martin <me+cratelyn@katelyn.world>
---
 src/collector.rs     |  8 ++++----
 src/encoding/text.rs |  2 +-
 src/registry.rs      | 10 +++++-----
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/collector.rs b/src/collector.rs
index 66270c41..eb377514 100644
--- a/src/collector.rs
+++ b/src/collector.rs
@@ -22,20 +22,20 @@ use crate::encoding::DescriptorEncoder;
 /// struct MyCollector {}
 ///
 /// impl Collector for MyCollector {
-///     fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
+///     fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error> {
 ///         let counter = ConstCounter::new(42u64);
-///         let metric_encoder = encoder.encode_descriptor(
+///         let mut metric_encoder = encoder.encode_descriptor(
 ///             "my_counter",
 ///             "some help",
 ///             None,
 ///             counter.metric_type(),
 ///         )?;
-///         counter.encode(metric_encoder)?;
+///         counter.encode(&mut metric_encoder)?;
 ///         Ok(())
 ///     }
 /// }
 /// ```
 pub trait Collector: std::fmt::Debug + Send + Sync + 'static {
     /// Once the [`Collector`] is registered, this method is called on each scrape.
-    fn encode(&self, encoder: DescriptorEncoder) -> Result<(), std::fmt::Error>;
+    fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error>;
 }
diff --git a/src/encoding/text.rs b/src/encoding/text.rs
index ca44aa97..aaac9cc5 100644
--- a/src/encoding/text.rs
+++ b/src/encoding/text.rs
@@ -1077,7 +1077,7 @@ mod tests {
         impl crate::collector::Collector for Collector {
             fn encode(
                 &self,
-                mut encoder: crate::encoding::DescriptorEncoder,
+                encoder: &mut crate::encoding::DescriptorEncoder,
             ) -> Result<(), std::fmt::Error> {
                 let counter = crate::metrics::counter::ConstCounter::new(42u64);
                 let mut metric_encoder = encoder.encode_descriptor(
diff --git a/src/registry.rs b/src/registry.rs
index 77653e22..d1be85ad 100644
--- a/src/registry.rs
+++ b/src/registry.rs
@@ -184,15 +184,15 @@ impl Registry {
     /// struct MyCollector {}
     ///
     /// impl Collector for MyCollector {
-    ///     fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
+    ///     fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error> {
     ///         let counter = ConstCounter::new(42u64);
-    ///         let metric_encoder = encoder.encode_descriptor(
+    ///         let mut metric_encoder = encoder.encode_descriptor(
     ///             "my_counter",
     ///             "some help",
     ///             None,
     ///             counter.metric_type(),
     ///         )?;
-    ///         counter.encode(metric_encoder)?;
+    ///         counter.encode(&mut metric_encoder)?;
     ///         Ok(())
     ///     }
     /// }
@@ -300,9 +300,9 @@ impl Registry {
         }
 
         for collector in self.collectors.iter() {
-            let descriptor_encoder =
+            let mut descriptor_encoder =
                 encoder.with_prefix_and_labels(self.prefix.as_ref(), &self.labels);
-            collector.encode(descriptor_encoder)?;
+            collector.encode(&mut descriptor_encoder)?;
         }
 
         for registry in self.sub_registries.iter() {