Skip to content

Commit 751d643

Browse files
committed
fix(pulling_gauge): reduce indirection
Previously we stored an `Arc<Box<dyn Fn()>>`, but we can directly store the function in the `Arc`. We could also take an `impl Fn() -> f64 + Send + Sync` instead of a `Box<dyn Fn() -> f64 + Send + Sync>` and do the allocation inside `new`: ```rust impl PullingGauge { pub fn new<S1: Into<String>, S2: Into<String>>( name: S1, help: S2, value: impl Fn() -> f64 + Send + Sync, ) -> crate::Result<Self> { 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<dyn Fn()>` implements `Fn()`, but it would make it less efficient since you are now storing a `Box` inside the `Arc`.
1 parent 1d3174b commit 751d643

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/pulling_gauge.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77

88
/// A [Gauge] that returns the value from a provided function on every collect run.
99
///
10-
/// This metric is the equivalant of Go's
10+
/// This metric is the equivalent of Go's
1111
/// <https://pkg.go.dev/github.com/prometheus/[email protected]/prometheus#GaugeFunc>
1212
///
1313
/// # Examples
@@ -28,7 +28,7 @@ use crate::{
2828
#[derive(Clone)]
2929
pub struct PullingGauge {
3030
desc: crate::core::Desc,
31-
value: Arc<Box<dyn Fn() -> f64 + Send + Sync>>,
31+
value: Arc<dyn Fn() -> f64 + Send + Sync>,
3232
}
3333

3434
impl fmt::Debug for PullingGauge {
@@ -48,7 +48,7 @@ impl PullingGauge {
4848
value: Box<dyn Fn() -> f64 + Send + Sync>,
4949
) -> crate::Result<Self> {
5050
Ok(PullingGauge {
51-
value: Arc::new(value),
51+
value: Arc::from(value),
5252
desc: crate::core::Desc::new(name.into(), help.into(), Vec::new(), HashMap::new())?,
5353
})
5454
}

0 commit comments

Comments
 (0)