Skip to content
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::from_ascii_to_digit #96110

Closed
wants to merge 1 commit into from
Closed

Add u8::from_ascii_to_digit #96110

wants to merge 1 commit into from

Conversation

gimbling-away
Copy link
Contributor

@gimbling-away gimbling-away commented Apr 16, 2022

Fixes #95969

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 16, 2022
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @scottmcm

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 16, 2022
@gimbling-away
Copy link
Contributor Author

r? rust-lang/libs-api

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 16, 2022
@rust-highfive rust-highfive assigned yaahc and unassigned scottmcm Apr 16, 2022
@rustbot rustbot removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Apr 16, 2022
@gimbling-away gimbling-away changed the title Add the u8::to_ascii_char method feat: Add the u8::to_ascii_char method Apr 16, 2022
@gimbling-away gimbling-away changed the title feat: Add the u8::to_ascii_char method feat: Add the u8::to_ascii_char method (#95969) Apr 16, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Stargateur
Copy link
Contributor

Stargateur commented Apr 16, 2022

That not what is asked, the point of to_digit() of char is to handle radix. This simple cast u8 into character. This requested feature is to skip the cast of u8 to char, it's simple a shortcut (that also remove the strange i32 of to_digit).

The signature the issue is looking for is:

const fn to_ascii_digit(self, radix: u8) -> Option<Self>;
// or 
const fn from_ascii_digit(self, radix: u8) -> Option<Self>;
// I prefer this one cause self is admited to be a ascii_digit we when to convert to u8 (as the issue is looking for)
// to_ascii_digit look more like the reverse of what the issue is looking for

panic for radix > 36.

@hniksic
Copy link
Contributor

hniksic commented Apr 16, 2022

In addition to what Stargateur said, I suggest that the result be an option for consistency with to_digit, and because only one failure mode is possible.

@gimbling-away
Copy link
Contributor Author

That not what is asked, the point of to_digit() of char is to handle radix. This simple cast u8 into character. This requested feature is to skip the cast of u8 to char, it's simple a shortcut (that also remove the strange i32 of to_digit).

The signature the issue is looking for is:

const fn to_ascii_digit(self, radix: u8) -> Option<Self>;
// or 
const fn from_ascii_digit(self, radix: u8) -> Option<Self>;
// I prefer this one cause self is admited to be a ascii_digit we when to convert to u8 (as the issue is looking for)
// to_ascii_digit look more like the reverse of what the issue is looking for

panic for radix > 36.

Ahh. My bad.

I'll look into this in a minute, thanks so much for the correction 👍

@gimbling-away gimbling-away changed the title feat: Add the u8::to_ascii_char method (#95969) [Still in work] add u8::to_ascii_digit (#95969) Apr 16, 2022
@gimbling-away
Copy link
Contributor Author

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 16, 2022
@gimbling-away gimbling-away marked this pull request as draft April 16, 2022 16:43
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@gimbling-away
Copy link
Contributor Author

[The force push was a squash]

@gimbling-away gimbling-away marked this pull request as ready for review April 17, 2022 11:00
@gimbling-away gimbling-away changed the title [Still in work] add u8::to_ascii_digit (#95969) Add u8::to_ascii_digit (#95969) Apr 17, 2022
@Stargateur
Copy link
Contributor

Stargateur commented Apr 28, 2022

that why I advice from_ascii_digit in #96110 (comment), I totally agree it's unclear

@gimbling-away
Copy link
Contributor Author

From the name it's not clear to me what direction this conversion goes.

Does b'5'.to_ascii_digit() give me 5? Or does 5u8.to_ascii_digit() give me b'5'?

The 'to ascii' in the name makes me think this returns an ascii character (b'5'), but it seems to do the opposite.

Agreed, the name is a bit confusing.

Renaming to from_ascii_digit 👍🏻

@gimbling-away gimbling-away changed the title Add u8::to_ascii_digit (#95969) Add u8::from_ascii_digit (#95969) Apr 29, 2022
@hniksic
Copy link
Contributor

hniksic commented Apr 29, 2022

Renaming to from_ascii_digit

Please note that the new method is supposed to parallel char::to_digit(). Calling it from_ascii_digit() sounds like it does the inverse, and has the potential to introduce confusion.

@Stargateur
Copy link
Contributor

@hniksic char::to_digit() make sense cause it's a char we convert to a digit u32, if we name this u8::to_digit it would be unclear what the function do, cause u8 is not a character, digit::to_digit() doesn't make sense. I think from_ form is better unless you have a better proposition.

@yaahc
Copy link
Member

yaahc commented Apr 29, 2022

I'm reassigning this PR because I'm taking a break from the review rotation for a little while. Thank you for your patience.

r? rust-lang/libs-api

@rust-highfive rust-highfive assigned dtolnay and unassigned yaahc Apr 29, 2022
@hniksic
Copy link
Contributor

hniksic commented Apr 30, 2022

@Stargateur I see your point. Maybe from_ascii_to_digit()? That would show what is being performed (conversion of ASCII byte to a digit in the specified base) and reference char::to_digit().

@gimbling-away
Copy link
Contributor Author

from_ascii_to_digit SGTM

Although I'd like to hear other people's takes on it before changing it =D

@gimbling-away
Copy link
Contributor Author

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 30, 2022
@gimbling-away gimbling-away reopened this May 4, 2022
@gimbling-away
Copy link
Contributor Author

Renamed to from_ascii_to_digit

@gimbling-away
Copy link
Contributor Author

@rustbot ready

@dtolnay Mind taking a look? :)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 19, 2022
@dtolnay dtolnay changed the title Add u8::from_ascii_digit (#95969) Add u8::from_ascii_to_digit May 23, 2022
@dtolnay
Copy link
Member

dtolnay commented May 23, 2022

@rust-lang/libs-api:
@rfcbot fcp close

I think I would prefer not to add this method.

The behavior of the proposed method is that ascii.from_ascii_to_digit(radix) is equivalent to (ascii as char).to_digit(radix) i.e. "from_ascii" refers to u8 as char and "to_digit" refers to char::to_digit, except that the new method has some extra casts thrown on top. In particular the proposed method takes the radix as u8 (but must be at most 36) and returns its result as u8 (but will be at most radix-1) whereas the existing char::to_digit takes its radix as u32 (must be at most 36) and returns u32 (at most radix-1).

Overall the benefit gained by giving this a name from_ascii_to_digit is not compelling to me. I find u8 as char a clearer way to conceptualize what is involved in this operation. We take a byte, interpret it as an ascii code, and get its digit value in a particular radix if it's valid in that radix.

As a counterproposal to the contributor, if they are interested, I would suggest to explore what a more fleshed out core::ascii would look like. Is there a useful notion of "ascii digit" as a type, which is able to make common patterns of ascii-manipulating code clearer to express? Are const generics useful for constraining a radix on a digit? It might turn out that none of this results in adding anything to core::ascii because our existing set of methods turn out to be hard to beat, but IMO it's a more promising approach than adding additional u8 methods.

@rfcbot
Copy link

rfcbot commented May 23, 2022

Team member @dtolnay has proposed to close this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. labels May 23, 2022
@gimbling-away
Copy link
Contributor Author

gimbling-away commented May 23, 2022

The behavior of the proposed method is that ascii.from_ascii_to_digit(radix) is equivalent to (ascii as char).to_digit(radix) i.e. "from_ascii" refers to u8 as char and "to_digit" refers to char::to_digit, except that the new method has some extra casts thrown on top. In particular the proposed method takes the radix as u8 (but must be at most 36) and returns its result as u8 (but will be at most radix-1) whereas the existing char::to_digit takes its radix as u32 (must be at most 36) and returns u32 (at most radix-1).

Just to make it clear, feedback regarding the implementation details, I'd be glad to consider and improve upon it. @dtolnay

However, I do agree that a more fleshed dedicated module to ASCII utilities might be better rather than pushing more methods into u8.

@scottmcm
Copy link
Member

👍 to closing in favour of something else later.

Having an AsciiChar in core sounds great to me. I think having more intentional types would make a huge difference for this api -- as it is it's very unclear to me from the name exactly which kinds of digits it refers to. Since we already have char::{to|from}_digit, if there could be AsciiChar::{to|from}_digit that would seem like a nice way.

(Such a type could even have a validity invariant of being actually ascii...)

@rfcbot rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. labels Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add u8::to_ascii_digit()