Skip to content

Commit 2649447

Browse files
authored
refactor(family): introduce ConstFamily type (#138)
Signed-off-by: Max Inden <[email protected]>
1 parent 85033bf commit 2649447

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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.21.0]
8+
9+
### Changed
10+
11+
- Replace `impl EncodeMetric for RefCell<T>` with a new type `ConstFamily` implementing `EncodeMetric`.
12+
713
## [0.20.0]
814

915
### Added

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.20.0"
3+
version = "0.21.0"
44
authors = ["Max Inden <[email protected]>"]
55
edition = "2021"
66
description = "Open Metrics client library allowing users to natively instrument applications."

src/metrics/family.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,31 @@ where
327327
}
328328
}
329329

330-
impl<S: EncodeLabelSet, M: EncodeMetric + TypedMetric, T: Iterator<Item = (S, M)>> EncodeMetric
331-
for RefCell<T>
330+
/// As a [`Family`], but constant, meaning it cannot change once created.
331+
///
332+
/// Needed for advanced use-cases, e.g. in combination with [`Collector`](crate::collector::Collector).
333+
///
334+
/// Note that a [`ConstFamily`], given that it is based on an [`Iterator`], can
335+
/// only be [`EncodeMetric::encode`]d once. While consecutive
336+
/// [`EncodeMetric::encode`] calls won't panic, they won't return any metrics as
337+
/// the provided [`Iterator`] will return [`Iterator::next`] [`None`]. Thus you
338+
/// should not return the same [`ConstFamily`] in more than one
339+
/// [`Collector::collect`](crate::collector::Collector::collect) calls.
340+
#[derive(Debug, Default)]
341+
pub struct ConstFamily<I>(RefCell<I>);
342+
343+
impl<I> ConstFamily<I> {
344+
/// Creates a new [`ConstFamily`].
345+
pub fn new(iter: I) -> Self {
346+
Self(RefCell::new(iter))
347+
}
348+
}
349+
350+
impl<S: EncodeLabelSet, M: EncodeMetric + TypedMetric, I: Iterator<Item = (S, M)>> EncodeMetric
351+
for ConstFamily<I>
332352
{
333353
fn encode(&self, mut encoder: MetricEncoder<'_, '_>) -> Result<(), std::fmt::Error> {
334-
let mut iter = self.borrow_mut();
354+
let mut iter = self.0.borrow_mut();
335355

336356
for (label_set, m) in iter.by_ref() {
337357
let encoder = encoder.encode_family(&label_set)?;

0 commit comments

Comments
 (0)