@@ -327,11 +327,31 @@ where
327
327
}
328
328
}
329
329
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 >
332
352
{
333
353
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 ( ) ;
335
355
336
356
for ( label_set, m) in iter. by_ref ( ) {
337
357
let encoder = encoder. encode_family ( & label_set) ?;
0 commit comments