From ffb1471447e20b92451622567f3b9a2dc0ccc19b Mon Sep 17 00:00:00 2001 From: koushiro Date: Sun, 10 Nov 2024 21:10:17 +0800 Subject: [PATCH 1/2] chore(encoding): adjust order of some impls Signed-off-by: koushiro --- src/encoding.rs | 173 ++++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 86 deletions(-) diff --git a/src/encoding.rs b/src/encoding.rs index 9b2e497..6f3f2ef 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -206,12 +206,6 @@ pub trait EncodeLabelSet { fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error>; } -impl<'a> From> for LabelSetEncoder<'a> { - fn from(e: text::LabelSetEncoder<'a>) -> Self { - Self(LabelSetEncoderInner::Text(e)) - } -} - /// Encoder for a label set. #[derive(Debug)] pub struct LabelSetEncoder<'a>(LabelSetEncoderInner<'a>); @@ -223,6 +217,12 @@ enum LabelSetEncoderInner<'a> { Protobuf(protobuf::LabelSetEncoder<'a>), } +impl<'a> From> for LabelSetEncoder<'a> { + fn from(e: text::LabelSetEncoder<'a>) -> Self { + Self(LabelSetEncoderInner::Text(e)) + } +} + #[cfg(feature = "protobuf")] impl<'a> From> for LabelSetEncoder<'a> { fn from(e: protobuf::LabelSetEncoder<'a>) -> Self { @@ -237,6 +237,42 @@ impl LabelSetEncoder<'_> { } } +impl EncodeLabelSet for [T; N] { + fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + self.as_ref().encode(encoder) + } +} + +impl EncodeLabelSet for &[T] { + fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + if self.is_empty() { + return Ok(()); + } + + for label in self.iter() { + label.encode(encoder.encode_label())? + } + + Ok(()) + } +} + +impl EncodeLabelSet for Vec { + fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + self.as_slice().encode(encoder) + } +} + +/// Uninhabited type to represent the lack of a label set for a metric +#[derive(Debug)] +pub enum NoLabelSet {} + +impl EncodeLabelSet for NoLabelSet { + fn encode(&self, _encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { + Ok(()) + } +} + /// An encodable label. pub trait EncodeLabel { /// Encode oneself into the given encoder. @@ -247,10 +283,6 @@ pub trait EncodeLabel { #[derive(Debug)] pub struct LabelEncoder<'a>(LabelEncoderInner<'a>); -/// Uninhabited type to represent the lack of a label set for a metric -#[derive(Debug)] -pub enum NoLabelSet {} - #[derive(Debug)] enum LabelEncoderInner<'a> { Text(text::LabelEncoder<'a>), @@ -283,6 +315,21 @@ impl LabelEncoder<'_> { } } +impl EncodeLabel for (K, V) { + fn encode(&self, mut encoder: LabelEncoder) -> Result<(), std::fmt::Error> { + let (key, value) = self; + + let mut label_key_encoder = encoder.encode_label_key()?; + key.encode(&mut label_key_encoder)?; + + let mut label_value_encoder = label_key_encoder.encode_label_value()?; + value.encode(&mut label_value_encoder)?; + label_value_encoder.finish()?; + + Ok(()) + } +} + /// An encodable label key. pub trait EncodeLabelKey { /// Encode oneself into the given encoder. @@ -330,52 +377,6 @@ impl<'a> LabelKeyEncoder<'a> { ) } } -impl EncodeLabelSet for [T; N] { - fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { - self.as_ref().encode(encoder) - } -} - -impl EncodeLabelSet for &[T] { - fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { - if self.is_empty() { - return Ok(()); - } - - for label in self.iter() { - label.encode(encoder.encode_label())? - } - - Ok(()) - } -} - -impl EncodeLabelSet for Vec { - fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { - self.as_slice().encode(encoder) - } -} - -impl EncodeLabelSet for NoLabelSet { - fn encode(&self, _encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> { - Ok(()) - } -} - -impl EncodeLabel for (K, V) { - fn encode(&self, mut encoder: LabelEncoder) -> Result<(), std::fmt::Error> { - let (key, value) = self; - - let mut label_key_encoder = encoder.encode_label_key()?; - key.encode(&mut label_key_encoder)?; - - let mut label_value_encoder = label_key_encoder.encode_label_value()?; - value.encode(&mut label_value_encoder)?; - label_value_encoder.finish()?; - - Ok(()) - } -} impl EncodeLabelKey for &str { fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> { @@ -433,6 +434,13 @@ pub trait EncodeLabelValue { #[derive(Debug)] pub struct LabelValueEncoder<'a>(LabelValueEncoderInner<'a>); +#[derive(Debug)] +enum LabelValueEncoderInner<'a> { + Text(text::LabelValueEncoder<'a>), + #[cfg(feature = "protobuf")] + Protobuf(protobuf::LabelValueEncoder<'a>), +} + impl<'a> From> for LabelValueEncoder<'a> { fn from(e: text::LabelValueEncoder<'a>) -> Self { LabelValueEncoder(LabelValueEncoderInner::Text(e)) @@ -446,11 +454,10 @@ impl<'a> From> for LabelValueEncoder<'a> { } } -#[derive(Debug)] -enum LabelValueEncoderInner<'a> { - Text(text::LabelValueEncoder<'a>), - #[cfg(feature = "protobuf")] - Protobuf(protobuf::LabelValueEncoder<'a>), +impl std::fmt::Write for LabelValueEncoder<'_> { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + for_both_mut!(self, LabelValueEncoderInner, e, e.write_str(s)) + } } impl LabelValueEncoder<'_> { @@ -460,12 +467,6 @@ impl LabelValueEncoder<'_> { } } -impl std::fmt::Write for LabelValueEncoder<'_> { - fn write_str(&mut self, s: &str) -> std::fmt::Result { - for_both_mut!(self, LabelValueEncoderInner, e, e.write_str(s)) - } -} - impl EncodeLabelValue for &str { fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> { encoder.write_str(self)?; @@ -678,6 +679,19 @@ enum CounterValueEncoderInner<'a> { Protobuf(protobuf::CounterValueEncoder<'a>), } +impl<'a> From> for CounterValueEncoder<'a> { + fn from(e: text::CounterValueEncoder<'a>) -> Self { + CounterValueEncoder(CounterValueEncoderInner::Text(e)) + } +} + +#[cfg(feature = "protobuf")] +impl<'a> From> for CounterValueEncoder<'a> { + fn from(e: protobuf::CounterValueEncoder<'a>) -> Self { + CounterValueEncoder(CounterValueEncoderInner::Protobuf(e)) + } +} + impl CounterValueEncoder<'_> { fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> { for_both_mut!(self, CounterValueEncoderInner, e, e.encode_f64(v)) @@ -718,19 +732,6 @@ impl EncodeExemplarValue for u32 { } } -impl<'a> From> for CounterValueEncoder<'a> { - fn from(e: text::CounterValueEncoder<'a>) -> Self { - CounterValueEncoder(CounterValueEncoderInner::Text(e)) - } -} - -#[cfg(feature = "protobuf")] -impl<'a> From> for CounterValueEncoder<'a> { - fn from(e: protobuf::CounterValueEncoder<'a>) -> Self { - CounterValueEncoder(CounterValueEncoderInner::Protobuf(e)) - } -} - /// Encoder for an exemplar value. #[derive(Debug)] pub struct ExemplarValueEncoder<'a>(ExemplarValueEncoderInner<'a>); @@ -742,12 +743,6 @@ enum ExemplarValueEncoderInner<'a> { Protobuf(protobuf::ExemplarValueEncoder<'a>), } -impl ExemplarValueEncoder<'_> { - fn encode(&mut self, v: f64) -> Result<(), std::fmt::Error> { - for_both_mut!(self, ExemplarValueEncoderInner, e, e.encode(v)) - } -} - impl<'a> From> for ExemplarValueEncoder<'a> { fn from(e: text::ExemplarValueEncoder<'a>) -> Self { ExemplarValueEncoder(ExemplarValueEncoderInner::Text(e)) @@ -760,3 +755,9 @@ impl<'a> From> for ExemplarValueEncoder<'a> { ExemplarValueEncoder(ExemplarValueEncoderInner::Protobuf(e)) } } + +impl ExemplarValueEncoder<'_> { + fn encode(&mut self, v: f64) -> Result<(), std::fmt::Error> { + for_both_mut!(self, ExemplarValueEncoderInner, e, e.encode(v)) + } +} From 96796bc35e7cacb1d10e964b249bec3a28e215d8 Mon Sep 17 00:00:00 2001 From: koushiro Date: Sun, 10 Nov 2024 21:13:00 +0800 Subject: [PATCH 2/2] feat(encoding): impl EncodeLabelValue for bool Signed-off-by: koushiro --- CHANGELOG.md | 7 ++++++- src/encoding.rs | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c398e07..945d128 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,8 +18,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Implement `Atomic` for `AtomicU64` for gauges. See [PR 226]. -[PR 226]: https://github.com/prometheus/client_rust/pull/198 +- Implement `EnableLabelValue` for `bool`. + See [PR 237] + +[PR 173]: https://github.com/prometheus/client_rust/pull/173 [PR 198]: https://github.com/prometheus/client_rust/pull/198 +[PR 226]: https://github.com/prometheus/client_rust/pull/226 +[PR 237]: https://github.com/prometheus/client_rust/pull/237 ### Added diff --git a/src/encoding.rs b/src/encoding.rs index 6f3f2ef..9e2acac 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -537,6 +537,12 @@ where } } +impl EncodeLabelValue for bool { + fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> { + encoder.write_str(if *self { "true" } else { "false" }) + } +} + macro_rules! impl_encode_label_value_for_integer { ($($t:ident),*) => {$( impl EncodeLabelValue for $t {