Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ 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.24.1]

### Fixed

- `EncodeGaugeValue`, `EncodeCounterValue` and `EncodeExemplarValue` now use
fewer `as` casts in their implementation. This caught an issue where
`EncodeGaugeValue` would not error when encoding some `u64`s that don't fit
in a `i64`. See [PR 281].

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

## [0.24.0]

### Added
Expand Down
22 changes: 9 additions & 13 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,9 @@ impl EncodeGaugeValue for i64 {
impl EncodeGaugeValue for u64 {
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
// Between forcing end users to do endless as i64 for things that are
// clearly u64 and having one error case for rarely used protobuf when
// a gauge is set to u64::MAX, the latter seems like the right choice.
if *self == u64::MAX {
return Err(std::fmt::Error);
}

encoder.encode_i64(*self as i64)
// clearly valid i64 and having one error case for rarely used protobuf when
// a gauge is set to >i64::MAX, the latter seems like the right choice.
encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?)
}
}

Expand All @@ -619,13 +615,13 @@ impl EncodeGaugeValue for f64 {

impl EncodeGaugeValue for i32 {
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_i64(*self as i64)
encoder.encode_i64(i64::from(*self))
}
}

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

Expand Down Expand Up @@ -687,13 +683,13 @@ impl EncodeCounterValue for f64 {

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

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

Expand Down Expand Up @@ -751,13 +747,13 @@ impl EncodeExemplarValue for u64 {

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

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

Expand Down
Loading