client_rust icon indicating copy to clipboard operation
client_rust copied to clipboard

src/registry: Consider using Into for metric when registering

Open mxinden opened this issue 4 years ago • 0 comments

With the diff below, one would not need to Box a metric before registering it with a Registry<Box<M>>. The downside is, that it makes it harder for Rust to infer M.

diff --git a/src/lib.rs b/src/lib.rs
index d39b566..d86d2dd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -52,7 +52,7 @@
 //!   "http_requests",
 //!   // And the metric help text.
 //!   "Number of HTTP requests received",
-//!   Box::new(http_requests.clone()),
+//!   http_requests.clone(),
 //! );
 //!
 //! // Somewhere in your business logic record a single HTTP GET request.
diff --git a/src/registry.rs b/src/registry.rs
index be9fd18..3033483 100644
--- a/src/registry.rs
+++ b/src/registry.rs
@@ -97,8 +97,8 @@ impl<M> Registry<M> {
     ///
     /// registry.register("my_counter", "This is my counter", counter.clone());
     /// ```
-    pub fn register<N: Into<String>, H: Into<String>>(&mut self, name: N, help: H, metric: M) {
-        self.priv_register(name, help, metric, None)
+    pub fn register<N: Into<String>, H: Into<String>, IM: Into<M>>(&mut self, name: N, help: H, metric: IM) {
+        self.priv_register(name, help, metric.into(), None)
     }
 
     /// Register a metric with the [`Registry`] specifying the metric's unit.

mxinden avatar Mar 12 '21 21:03 mxinden