From 40df0341b749d561a5f047bb9dd20fb7bcaa71fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Mon, 25 Aug 2025 17:40:05 +0200 Subject: [PATCH] fix(pulling_gauge): reduce indirection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously we stored an `Arc>`, but we can directly store the function in the `Arc`. We could also take an `impl Fn() -> f64 + Send + Sync` instead of a `Box f64 + Send + Sync>` and do the allocation inside `new`: ```rust impl PullingGauge { pub fn new, S2: Into>( name: S1, help: S2, value: impl Fn() -> f64 + Send + Sync, ) -> crate::Result { Ok(PullingGauge { value: Arc::new(value), desc: crate::core::Desc::new(name.into(), help.into(), Vec::new(), HashMap::new())?, }) } } ``` This is more efficient and lets you write the closure directly on the calling site: ```rust // Previously let pulling_gauge = PullingGauge::new("foo", "bar", Box::new(|| static_vec.len() as f64)); // Now let pulling_gauge = PullingGauge::new("foo", "bar", || static_vec.len() as f64); ``` This wouldn't be a breaking change since `Box` implements `Fn()`, but it would make it less efficient since you are now storing a `Box` inside the `Arc`. Signed-off-by: Jalil David Salamé Messina --- src/pulling_gauge.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pulling_gauge.rs b/src/pulling_gauge.rs index 6f7e1444..bce0da1f 100644 --- a/src/pulling_gauge.rs +++ b/src/pulling_gauge.rs @@ -7,7 +7,7 @@ use crate::{ /// A [Gauge] that returns the value from a provided function on every collect run. /// -/// This metric is the equivalant of Go's +/// This metric is the equivalent of Go's /// /// /// # Examples @@ -28,7 +28,7 @@ use crate::{ #[derive(Clone)] pub struct PullingGauge { desc: crate::core::Desc, - value: Arc f64 + Send + Sync>>, + value: Arc f64 + Send + Sync>, } impl fmt::Debug for PullingGauge { @@ -48,7 +48,7 @@ impl PullingGauge { value: Box f64 + Send + Sync>, ) -> crate::Result { Ok(PullingGauge { - value: Arc::new(value), + value: Arc::from(value), desc: crate::core::Desc::new(name.into(), help.into(), Vec::new(), HashMap::new())?, }) }