diff --git a/CHANGELOG.md b/CHANGELOG.md index 526489c..f91078f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `EncodeGaugeValue` would not error when encoding some `u64`s that don't fit in a `i64`. See [PR 281]. +- `EncodeGaugeValue` is implemented for `usize` and `isize`, and + `EncodeCounterValue` is implemented for `usize`. See [PR 282]. + [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 9eb18bd..67a5812 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -607,6 +607,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) @@ -675,6 +690,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)