-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Introduce impl TryFrom for Number that succeeds iff the value is within the safe range #3847
Conversation
…in the safe range
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.
A design question I had while writing this: should the Error type be just the passed-in value? It seems to be the way other TryFrom implementations in js-sys are currently done, so I went ahead with the same idea here.
Where did you see that in js-sys
?
I think I'm also in favor of doing the conversion like this, but FWIW alternatively we could make it infallible by using -/+infinity when the number is not in range.
Please add a changelog entry as well.
The The argument against doing the same here would be that all those impls convert from That would line up with what The closest analogy to |
Thank you for your feedback! I have:
Hopefully this is good to go! :) |
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.
- switched the code to use a
TryFromIntError
type, copy-pasted from the libstd, the only change being that I didn't importError
globally. We apparently cannot construct the libstd's variant easily, and doing eg.u32::try_from(u64::MAX)?
to return it would look like too much of a hack for me — let me know if you'd have preferred that approach
Yeah, my main concern with using Stds TryFromIntError
type is that we aren't really in control of what it displays exactly so it could become confusing in the future. Though I'm not happy with adding a dedicated error type for this I don't have a good alternative suggestion.
So except the couple of nits its LGTM.
Co-authored-by: daxpedda <daxpedda@gmail.com>
Co-authored-by: daxpedda <daxpedda@gmail.com>
Co-authored-by: daxpedda <daxpedda@gmail.com>
@daxpedda Looks like the CI started failing like the one I recently submitted a fix for on web-time. Here, it looks like the check is not required, so I think it'd probably be better to land this without the CI changes, for clarity? And to land the CI fixes independently, depending on whether you prefer bumping the msrv or pinning |
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.
No, I will go ahead and fix the CI first.
EDIT: See #3850.
Awesome, thank you! I just handled your review comment and merged master in, hopefully this is now good to go :) |
@Liamolucko would appreciate your approval here as well. |
As a counterpoint, raised by MaulingMonkey on discord: it could also make sense to round the number, so maybe TryFrom & co should not be implemented for Number?
OTOH, I personally think the added convenience is worth it, and by implementing TryFrom rather than From we make it explicit that the conversion is fallible, which should make it obvious that the behavior is "error out if the number is outside the safe range". Especially considering that
Number
in JavaScript is often used to describe integers in the safe range, probably much more often than it is actually used to describe floating point numbers.A design question I had while writing this: should the
Error
type be just the passed-in value? It seems to be the way otherTryFrom
implementations injs-sys
are currently done, so I went ahead with the same idea here.