Skip to content

Commit cdd1bd2

Browse files
committed
feat(encoding): encode usize and isize
Implement `EncodeGaugeValue` and 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 <[email protected]>
1 parent 6bb0b10 commit cdd1bd2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/encoding.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,21 @@ impl EncodeGaugeValue for u64 {
607607
}
608608
}
609609

610+
impl EncodeGaugeValue for isize {
611+
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
612+
// Is infallible for 32-bit or 64-bit platforms
613+
encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?)
614+
}
615+
}
616+
617+
impl EncodeGaugeValue for usize {
618+
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
619+
// For 32-bit platforms this is infallible, for 64-bit platforms the argument is the same
620+
// as the one for u64 values
621+
encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?)
622+
}
623+
}
624+
610625
impl EncodeGaugeValue for f64 {
611626
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
612627
encoder.encode_f64(*self)
@@ -675,6 +690,13 @@ impl EncodeCounterValue for u64 {
675690
}
676691
}
677692

693+
impl EncodeCounterValue for usize {
694+
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
695+
// Is infallible for 32-bit and 64-bit platforms
696+
encoder.encode_u64(u64::try_from(*self).map_err(|_err| std::fmt::Error)?)
697+
}
698+
}
699+
678700
impl EncodeCounterValue for f64 {
679701
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
680702
encoder.encode_f64(*self)

0 commit comments

Comments
 (0)