Skip to content

Commit 9a74e99

Browse files
authored
refactor(metrics/histogram): constructor accepts IntoIterator (#243)
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<I: Iterator> 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<f64>` 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: <https://doc.rust-lang.org/stable/std/iter/index.html#for-loops-and-intoiterator> 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 <[email protected]>
1 parent cbde2cb commit 9a74e99

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.23.1] - unreleased
8+
9+
### Changed
10+
11+
- `Histogram::new` now accepts an `IntoIterator` argument, rather than an `Iterator`.
12+
See [PR 243].
13+
14+
[PR 243]: https://github.com/prometheus/client_rust/pull/243
15+
716
## [0.23.0]
817

918
### Changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prometheus-client"
3-
version = "0.23.0"
3+
version = "0.23.1"
44
authors = ["Max Inden <[email protected]>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."

src/metrics/histogram.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::sync::Arc;
2828
/// let custom_buckets = [
2929
/// 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
3030
/// ];
31-
/// let histogram = Histogram::new(custom_buckets.into_iter());
31+
/// let histogram = Histogram::new(custom_buckets);
3232
/// histogram.observe(4.2);
3333
/// ```
3434
// TODO: Consider using atomics. See
@@ -57,7 +57,12 @@ pub(crate) struct Inner {
5757

5858
impl Histogram {
5959
/// Create a new [`Histogram`].
60-
pub fn new(buckets: impl Iterator<Item = f64>) -> Self {
60+
///
61+
/// ```rust
62+
/// # use prometheus_client::metrics::histogram::Histogram;
63+
/// let histogram = Histogram::new([10.0, 100.0, 1_000.0]);
64+
/// ```
65+
pub fn new(buckets: impl IntoIterator<Item = f64>) -> Self {
6166
Self {
6267
inner: Arc::new(RwLock::new(Inner {
6368
sum: Default::default(),

0 commit comments

Comments
 (0)