Add u8::parse_ascii_digit method#83447
Add u8::parse_ascii_digit method#83447coolreader18 wants to merge 3 commits intorust-lang:masterfrom
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
There was a problem hiding this comment.
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.
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_digitto u8, I think those names make sense; however, at the moment, all ofu8's char-like have an_asciiqualifier in their name. So, should we break that convention, or should we come up with a new one like{is,to}_ascii_digit_radixthat no longer has the same name as the equivalent method on char?