diff --git a/CHANGELOG.md b/CHANGELOG.md index 733e209..80654b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.24.1] +### Added + +- `EncodeGaugeValue` is implemented for `usize` and `isize`, and + `EncodeCounterValue` is implemented for `usize`. See [PR 282]. + ### Fixed - `EncodeGaugeValue`, `EncodeCounterValue` and `EncodeExemplarValue` now use @@ -25,6 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [PR 242]: https://github.com/prometheus/client_rust/pull/242 [PR 245]: https://github.com/prometheus/client_rust/pull/245 [PR 246]: https://github.com/prometheus/client_rust/pull/246 +[PR 281]: https://github.com/prometheus/client_rust/pull/281 +[PR 282]: https://github.com/prometheus/client_rust/pull/282 ## [0.24.0] diff --git a/src/encoding.rs b/src/encoding.rs index 46cc1b5..44ec0ef 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -618,6 +618,21 @@ impl EncodeGaugeValue for u64 { } } +impl EncodeGaugeValue for isize { + fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> { + // Is infallible for 32-bit or 64-bit platforms + encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?) + } +} + +impl EncodeGaugeValue for usize { + fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> { + // For 32-bit platforms this is infallible, for 64-bit platforms the argument is the same + // as the one for u64 values + encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?) + } +} + impl EncodeGaugeValue for f64 { fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> { encoder.encode_f64(*self) @@ -686,6 +701,13 @@ impl EncodeCounterValue for u64 { } } +impl EncodeCounterValue for usize { + fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> { + // Is infallible for 32-bit and 64-bit platforms + encoder.encode_u64(u64::try_from(*self).map_err(|_err| std::fmt::Error)?) + } +} + impl EncodeCounterValue for f64 { fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> { encoder.encode_f64(*self)