From 9993c64b37b0e7382727998c6f1e163401d6df43 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Fri, 1 Nov 2024 00:00:00 +0000 Subject: [PATCH] refactor(metrics/histogram): constructor accepts `IntoIterator` this is a very small, **non-breaking**, alteration to the signature of `Histogram`'s constructor. rather than accepting an `impl Iterator`, this commit changes the parameter of `Histogram::new()` to be an `impl IntoIterator` instead. this does not affect the existing contract because of the blanket `impl IntoIterator for I` implementation provided by the standard library. by accepting `IntoIterator` however, callers providing a collection such as a `[f64; 5]` array or a `Vec` vector do not need to invoke `into_iter()` themselves at the call-site. ```rust // now, constructing a histogram needn't involve `into_iter()`... use prometheus_client::metrics::histogram::Histogram; let histogram = Histogram::new([10.0, 100.0, 1_000.0]); ``` this leans on the same sugar used by `for {}` loops, see the relevant section of the `std::iter` documentation here: no changes are needed within `Histogram::new()` because we already call `into_iter()` on the provider iterator when appending `f64::MAX` and collecting the buckets into a `Vec<_>`. Signed-off-by: katelyn martin --- src/metrics/histogram.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/metrics/histogram.rs b/src/metrics/histogram.rs index 1e418ad9..a215a6d2 100644 --- a/src/metrics/histogram.rs +++ b/src/metrics/histogram.rs @@ -28,7 +28,7 @@ use std::sync::Arc; /// let custom_buckets = [ /// 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, /// ]; -/// let histogram = Histogram::new(custom_buckets.into_iter()); +/// let histogram = Histogram::new(custom_buckets); /// histogram.observe(4.2); /// ``` // TODO: Consider using atomics. See @@ -57,7 +57,12 @@ pub(crate) struct Inner { impl Histogram { /// Create a new [`Histogram`]. - pub fn new(buckets: impl Iterator) -> Self { + /// + /// ```rust + /// # use prometheus_client::metrics::histogram::Histogram; + /// let histogram = Histogram::new([10.0, 100.0, 1_000.0]); + /// ``` + pub fn new(buckets: impl IntoIterator) -> Self { Self { inner: Arc::new(RwLock::new(Inner { sum: Default::default(),