Skip to content

Commit

Permalink
chore(portability): Support u32 for Counters
Browse files Browse the repository at this point in the history
  • Loading branch information
navaati committed Jun 29, 2024
1 parent bf196d7 commit a7f6494
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ pub trait EncodeCounterValue {
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error>;
}

impl EncodeCounterValue for u32 {
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_u32(*self)
}
}

impl EncodeCounterValue for u64 {
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_u64(*self)
Expand All @@ -630,6 +636,10 @@ enum CounterValueEncoderInner<'a> {
}

impl<'a> CounterValueEncoder<'a> {
fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
for_both_mut!(self, CounterValueEncoderInner, e, e.encode_u32(v))
}

fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
for_both_mut!(self, CounterValueEncoderInner, e, e.encode_f64(v))
}
Expand Down
4 changes: 4 additions & 0 deletions src/encoding/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ pub(crate) struct CounterValueEncoder<'a> {
}

impl<'a> CounterValueEncoder<'a> {
pub fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
self.encode_u64(v as u64)
}

pub fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
*self.value = openmetrics_data_model::counter_value::Total::DoubleValue(v);
Ok(())
Expand Down
6 changes: 6 additions & 0 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ impl<'a> std::fmt::Debug for CounterValueEncoder<'a> {
}

impl<'a> CounterValueEncoder<'a> {
pub fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
self.writer.write_str(" ")?;
self.writer.write_str(itoa::Buffer::new().format(v))?;
Ok(())
}

pub fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
self.writer.write_str(" ")?;
self.writer.write_str(dtoa::Buffer::new().format(v))?;
Expand Down

0 comments on commit a7f6494

Please sign in to comment.