refactor(encoding): remove as casts#281
Merged
mxinden merged 1 commit intoprometheus:masterfrom Sep 2, 2025
Merged
Conversation
92911cf to
5d7b90b
Compare
Contributor
Author
|
Also created jalil-salame#1 as a follow up (adding |
mxinden
reviewed
Sep 2, 2025
Member
mxinden
left a comment
There was a problem hiding this comment.
Thanks for the fix! Good work.
Mind adding a changelog entry in CHANGELOG.md? If it doesn't exist yet, you can add [0.24.1] section.
There was a bug in the `impl EncodeGaugeValue for u64` implementation due to the `as i64` cast. The implementation only checked if `self == u64::MAX`, this is wrong since `u64::MAX` is not the only value that cannot be represented by an `i64`. The range of values is actually `self > i64::MAX`. `u64::MAX == 2^64 - 1` whereas `i64::MAX == 2^63 - 1`, this means there are `2^63` `u64` values that are not representable by an `i64`, not just `i64::MAX`. Delegating the checks to `i64::try_from(self)` ensures the logic is right. For this reason I also switched the rest of the implementations to use `N::from` instead of `as N` since the `N::from` function is only implemented for types whose conversion is infallible, this guarantees no such issues will ever happen. The only `as` cast left is `u64 as f64` in `EncodeExemplarValue`, this is not possible to remove since there is no `impl TryFrom<u64> for f64`. Signed-off-by: Jalil David Salamé Messina <[email protected]>
5d7b90b to
304356a
Compare
Contributor
Author
Done! |
mxinden
approved these changes
Sep 2, 2025
as as casts
as as castsas casts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a bug in the
impl EncodeGaugeValue for u64implementation due to theas i64cast.The implementation only checked if
self == u64::MAX, this is wrong sinceu64::MAXis not the only value that cannot be represented by ani64.The range of values is actually
self > i64::MAX.u64::MAX == 2^64 - 1whereasi64::MAX == 2^63 - 1, this means there are2^63u64values that are not representable by ani64, not justi64::MAX. Delegating the checks toi64::try_from(self)ensures the logic is right. For this reason I also switched the rest of the implementations to useN::frominstead ofas Nsince theN::fromfunction is only implemented for types whose conversion is infallible, this guarantees no such issues will ever happen.The only
ascast left isu64 as f64inEncodeExemplarValue, this is not possible to remove since there is noimpl TryFrom<u64> for f64.