Skip to content

Commit 276bb9c

Browse files
feat(encoding): encode usize and isize (#282)
Implement `EncodeGaugeValue` for `usize` and `isize`, and `EncodeCounterValue` for `usize`. This is very straight forward and the argument is the same as the one for `EncodeGaugeValue for u64`; if you don't do this the user code will get littered with `as i64`/`as u64`. Since these conversions are infallible for `32-bit` platforms, and have the same issues as the current `u64` impl for `64-bit` platforms, I don't think there is any problem with adding them. Signed-off-by: Jalil David Salamé Messina <jalil.salame@gmail.com> Signed-off-by: Max Inden <mail@max-inden.de> Co-authored-by: Max Inden <mail@max-inden.de>
1 parent 3942d06 commit 276bb9c

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [0.24.1]
88

9+
### Added
10+
11+
- `EncodeGaugeValue` is implemented for `usize` and `isize`, and
12+
`EncodeCounterValue` is implemented for `usize`. See [PR 282].
13+
914
### Fixed
1015

1116
- `EncodeGaugeValue`, `EncodeCounterValue` and `EncodeExemplarValue` now use
@@ -25,6 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2530
[PR 242]: https://github.com/prometheus/client_rust/pull/242
2631
[PR 245]: https://github.com/prometheus/client_rust/pull/245
2732
[PR 246]: https://github.com/prometheus/client_rust/pull/246
33+
[PR 281]: https://github.com/prometheus/client_rust/pull/281
34+
[PR 282]: https://github.com/prometheus/client_rust/pull/282
2835

2936
## [0.24.0]
3037

src/encoding.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,21 @@ impl EncodeGaugeValue for u64 {
618618
}
619619
}
620620

621+
impl EncodeGaugeValue for isize {
622+
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
623+
// Is infallible for 32-bit or 64-bit platforms
624+
encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?)
625+
}
626+
}
627+
628+
impl EncodeGaugeValue for usize {
629+
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
630+
// For 32-bit platforms this is infallible, for 64-bit platforms the argument is the same
631+
// as the one for u64 values
632+
encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?)
633+
}
634+
}
635+
621636
impl EncodeGaugeValue for f64 {
622637
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
623638
encoder.encode_f64(*self)
@@ -686,6 +701,13 @@ impl EncodeCounterValue for u64 {
686701
}
687702
}
688703

704+
impl EncodeCounterValue for usize {
705+
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
706+
// Is infallible for 32-bit and 64-bit platforms
707+
encoder.encode_u64(u64::try_from(*self).map_err(|_err| std::fmt::Error)?)
708+
}
709+
}
710+
689711
impl EncodeCounterValue for f64 {
690712
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
691713
encoder.encode_f64(*self)

0 commit comments

Comments
 (0)