Skip to content

Commit 4eecdc3

Browse files
authored
feat(encoding): implement EncodeLabel* for reference types (#188)
Signed-off-by: Andre Masella <[email protected]>
1 parent 987037c commit 4eecdc3

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ 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.22.1]
8+
9+
### Added
10+
11+
- Added `EncodeLabelValue` and `EncodeLabelKey` implementations for `Arc`,
12+
`Rc`, and `Box`.
13+
See [PR 188].
14+
15+
[PR 188]: https://github.com/prometheus/client_rust/pull/188
16+
717
## [0.22.0]
818

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

src/encoding.rs

+56
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use std::borrow::Cow;
99
use std::collections::HashMap;
1010
use std::fmt::Write;
1111
use std::ops::Deref;
12+
use std::rc::Rc;
13+
use std::sync::Arc;
1214

1315
#[cfg(feature = "protobuf")]
1416
pub mod protobuf;
@@ -389,6 +391,33 @@ impl<'a> EncodeLabelKey for Cow<'a, str> {
389391
}
390392
}
391393

394+
impl<T> EncodeLabelKey for Box<T>
395+
where
396+
for<'a> &'a T: EncodeLabelKey,
397+
{
398+
fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> {
399+
EncodeLabelKey::encode(&self.as_ref(), encoder)
400+
}
401+
}
402+
403+
impl<T> EncodeLabelKey for Arc<T>
404+
where
405+
for<'a> &'a T: EncodeLabelKey,
406+
{
407+
fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> {
408+
EncodeLabelKey::encode(&self.as_ref(), encoder)
409+
}
410+
}
411+
412+
impl<T> EncodeLabelKey for Rc<T>
413+
where
414+
for<'a> &'a T: EncodeLabelKey,
415+
{
416+
fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> {
417+
EncodeLabelKey::encode(&self.as_ref(), encoder)
418+
}
419+
}
420+
392421
/// An encodable label value.
393422
pub trait EncodeLabelValue {
394423
/// Encode oneself into the given encoder.
@@ -450,6 +479,33 @@ impl<'a> EncodeLabelValue for Cow<'a, str> {
450479
}
451480
}
452481

482+
impl<T> EncodeLabelValue for Box<T>
483+
where
484+
for<'a> &'a T: EncodeLabelValue,
485+
{
486+
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
487+
EncodeLabelValue::encode(&self.as_ref(), encoder)
488+
}
489+
}
490+
491+
impl<T> EncodeLabelValue for Arc<T>
492+
where
493+
for<'a> &'a T: EncodeLabelValue,
494+
{
495+
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
496+
EncodeLabelValue::encode(&self.as_ref(), encoder)
497+
}
498+
}
499+
500+
impl<T> EncodeLabelValue for Rc<T>
501+
where
502+
for<'a> &'a T: EncodeLabelValue,
503+
{
504+
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
505+
EncodeLabelValue::encode(&self.as_ref(), encoder)
506+
}
507+
}
508+
453509
impl EncodeLabelValue for f64 {
454510
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
455511
encoder.write_str(dtoa::Buffer::new().format(*self))

0 commit comments

Comments
 (0)