Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(encoding): implement EncodeLabelKey and EncodeLabelValue for Arc #188

Merged
merged 2 commits into from Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,15 @@ 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`.
mxinden marked this conversation as resolved.
Show resolved Hide resolved

[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