Commit 92911cf
committed
refactor(encoding): remove as many as casts as possible
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 <jalil.salame@gmail.com>1 parent 7844d86 commit 92911cf
1 file changed
+8
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
602 | 602 | | |
603 | 603 | | |
604 | 604 | | |
605 | | - | |
606 | | - | |
607 | | - | |
608 | | - | |
609 | | - | |
610 | | - | |
| 605 | + | |
| 606 | + | |
611 | 607 | | |
612 | 608 | | |
613 | 609 | | |
| |||
619 | 615 | | |
620 | 616 | | |
621 | 617 | | |
622 | | - | |
| 618 | + | |
623 | 619 | | |
624 | 620 | | |
625 | 621 | | |
626 | 622 | | |
627 | 623 | | |
628 | | - | |
| 624 | + | |
629 | 625 | | |
630 | 626 | | |
631 | 627 | | |
| |||
687 | 683 | | |
688 | 684 | | |
689 | 685 | | |
690 | | - | |
| 686 | + | |
691 | 687 | | |
692 | 688 | | |
693 | 689 | | |
694 | 690 | | |
695 | 691 | | |
696 | | - | |
| 692 | + | |
697 | 693 | | |
698 | 694 | | |
699 | 695 | | |
| |||
751 | 747 | | |
752 | 748 | | |
753 | 749 | | |
754 | | - | |
| 750 | + | |
755 | 751 | | |
756 | 752 | | |
757 | 753 | | |
758 | 754 | | |
759 | 755 | | |
760 | | - | |
| 756 | + | |
761 | 757 | | |
762 | 758 | | |
763 | 759 | | |
| |||
0 commit comments