Skip to content

Commit

Permalink
feat(encoding)!: EncodeExemplarValue does not consume `ExemplarValu…
Browse files Browse the repository at this point in the history
…eEncoder`

see #135.

this adjusts the parameter of `encode()` so that it only mutably borrows
the encoder.

Signed-off-by: katelyn martin <[email protected]>
  • Loading branch information
cratelyn committed Nov 18, 2024
1 parent e1e38ed commit 7d99951
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,29 +691,29 @@ impl<'a> CounterValueEncoder<'a> {
/// An encodable exemplar value.
pub trait EncodeExemplarValue {
/// Encode the given instance in the OpenMetrics text encoding.
fn encode(&self, encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error>;
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error>;
}

impl EncodeExemplarValue for f64 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self)
}
}

impl EncodeExemplarValue for u64 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self as f64)
}
}

impl EncodeExemplarValue for f32 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self as f64)
}
}

impl EncodeExemplarValue for u32 {
fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
fn encode(&self, encoder: &mut ExemplarValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode(*self as f64)
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/encoding/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,8 @@ impl<S: EncodeLabelSet, V: EncodeExemplarValue> TryFrom<&Exemplar<S, V>>

fn try_from(exemplar: &Exemplar<S, V>) -> Result<Self, Self::Error> {
let mut value = f64::default();
exemplar
.value
.encode(ExemplarValueEncoder { value: &mut value }.into())?;
let mut encoder = ExemplarValueEncoder { value: &mut value }.into();
exemplar.value.encode(&mut encoder)?;

let mut labels = vec![];
let mut encoder = LabelSetEncoder {
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl<'a> MetricEncoder<'a> {
.encode(&mut LabelSetEncoder::new(self.writer).into())?;
self.writer.write_str("} ")?;
exemplar.value.encode(
ExemplarValueEncoder {
&mut ExemplarValueEncoder {
writer: self.writer,
}
.into(),
Expand Down

0 comments on commit 7d99951

Please sign in to comment.