-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add u8::parse_ascii_digit method #83447
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
I don't know if the ship has already sailed with, as you mentioned, "mirroring" between
What you're really asking from the unsigned integer is, "if this integer was visually represented as a char, would it then look like a digit? This is double indirection and seems to overload the general type (
|
I'm not so fond of panics that could be avoided. So I'd like an API where the radix is a const generic argument (and generates panics at compile time). And/or an API where a wrong radix leads to a None result instead of a panic. But the first requires a new function (like slice::array_windows), and the second is probably not a possible change now? |
I'm not sure, I just copied the existing |
I agree it's better to keep the two consistent. But the question is if we can also change the other function, so both return None instead of raising a panic :) |
library/core/src/num/mod.rs
Outdated
/// ``` | ||
#[unstable(feature = "to_ascii_digit", issue = "83447")] | ||
#[inline] | ||
pub fn to_ascii_digit_radix(&self, radix: u32) -> Option<u32> { |
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 is the return value a u32
?
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.
Oh, I was unsure about this as well; it seems weird that the char version also operates on u32 since the max that can be accepted/returned is 36/35, but I figured maybe there was a reason other than than the fact that char==u32 (like the '32-bit is a good default choice' that rust ascribes to or something)
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.
Yeah we usually use u32
as a default for (unsigned) integers that don't get large. E.g. the exponent in pow
or the return value of count_zeros
.
I personally don't think this needs the (Bikeshedding acknowledged. The types involved seem like the more important question.) |
☔ The latest upstream changes (presumably #83664) made this pull request unmergeable. Please resolve the merge conflicts. |
Just by the name i was expecting this function to do the exact opposite: |
1bae087
to
c416b71
Compare
Hmm, yeah, that would be confusing as well. Maybe something with |
Renamed to |
@coolreader18 If you think this will take a while to bikeshed then you may want to close this PR for now and open a tracking issue, if only to spare yourself the effort of rebasing. :) In the meantime I'm going to recategorize this from "waiting on review" to "waiting on bikeshed". |
That makes sense, thanks. I don't mind rebasing/it can be put off until there's consensus, so hopefully it's fine if I keep this open. I feel pretty good about |
What if we mirrored Edit: Meh, no, that abstraction wouldn't be useful beyond digits. I think |
I would expect no radix arg since |
What is the difference between |
Same as the difference between |
We discussed this in today's @rust-lang/libs-api meeting. Even with the improved name, we still felt quite hesitant to have this as a method on (Note that some people in the meeting didn't want to have |
It's better to avoid "as" casts whenever possible. But this desire should be balanced with other desires, like to keep the stdlib smaller, etc :) |
☔ The latest upstream changes (presumably #93762) made this pull request unmergeable. Please resolve the merge conflicts. |
Closing this based on the meeting notes. Thanks for contributing. |
I'd appreciate some guidance from the libs team wrt naming, since I think there's a inconsistency in how u8 mirrors methods from char -- char and u8 currently have
is_ascii_digit
, but char also hasis_digit(radix)
. So, if we were to addis_digit
/to_digit
to u8, I think those names make sense; however, at the moment, all ofu8
's char-like have an_ascii
qualifier in their name. So, should we break that convention, or should we come up with a new one like{is,to}_ascii_digit_radix
that no longer has the same name as the equivalent method on char?