-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor int conversion #45
base: main
Are you sure you want to change the base?
refactor int conversion #45
Conversation
DoTheBestToGetTheBest
commented
Jan 8, 2024
- this PR refactors the to_safe_integer and to_u32 functions into a generic to_integer function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change? Looks more complicated than before. Introducing TryFrom is not needed in the original code
hello, yes i used tryfrom for fallible conversions when using tryfrom it attempts to convert one type into another, but unlike a direct cast, it can handle cases where the conversion might not be possible if you are not okey with TryFrom i could remove it :) thanks |
I'm primarily wondering what the advantage of the change is |
hey, so basically, it handles errors that can occur during type conversion ref : https://doc.rust-lang.org/std/convert/trait.TryFrom.html thanks:) |
Right, but we already checked
So the cast to u32 is always safe. What other errors can happen here that |
for example when converting a f64 to a smaller integer type , there's a risk of overflow. TryFrom safely handles these cases by returning an Err |