refactor(encoding): remove as casts
#281
Merged
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.