Skip to content

Commit

Permalink
feat(encoding): implement EncodeLabel* for reference types (#188)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Masella <andre@masella.name>
  • Loading branch information
apmasell committed Jan 30, 2024
1 parent 987037c commit 4eecdc3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.22.1]

### Added

- Added `EncodeLabelValue` and `EncodeLabelKey` implementations for `Arc`,
`Rc`, and `Box`.
See [PR 188].

[PR 188]: https://github.com/prometheus/client_rust/pull/188

## [0.22.0]

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "prometheus-client"
version = "0.22.0"
version = "0.22.1"
authors = ["Max Inden <mail@max-inden.de>"]
edition = "2021"
description = "Open Metrics client library allowing users to natively instrument applications."
Expand Down
56 changes: 56 additions & 0 deletions src/encoding.rs
Expand Up @@ -9,6 +9,8 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Write;
use std::ops::Deref;
use std::rc::Rc;
use std::sync::Arc;

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

impl<T> EncodeLabelKey for Box<T>
where
for<'a> &'a T: EncodeLabelKey,
{
fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> {
EncodeLabelKey::encode(&self.as_ref(), encoder)
}
}

impl<T> EncodeLabelKey for Arc<T>
where
for<'a> &'a T: EncodeLabelKey,
{
fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> {
EncodeLabelKey::encode(&self.as_ref(), encoder)
}
}

impl<T> EncodeLabelKey for Rc<T>
where
for<'a> &'a T: EncodeLabelKey,
{
fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> {
EncodeLabelKey::encode(&self.as_ref(), encoder)
}
}

/// An encodable label value.
pub trait EncodeLabelValue {
/// Encode oneself into the given encoder.
Expand Down Expand Up @@ -450,6 +479,33 @@ impl<'a> EncodeLabelValue for Cow<'a, str> {
}
}

impl<T> EncodeLabelValue for Box<T>
where
for<'a> &'a T: EncodeLabelValue,
{
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
EncodeLabelValue::encode(&self.as_ref(), encoder)
}
}

impl<T> EncodeLabelValue for Arc<T>
where
for<'a> &'a T: EncodeLabelValue,
{
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
EncodeLabelValue::encode(&self.as_ref(), encoder)
}
}

impl<T> EncodeLabelValue for Rc<T>
where
for<'a> &'a T: EncodeLabelValue,
{
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
EncodeLabelValue::encode(&self.as_ref(), encoder)
}
}

impl EncodeLabelValue for f64 {
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
encoder.write_str(dtoa::Buffer::new().format(*self))
Expand Down

0 comments on commit 4eecdc3

Please sign in to comment.