-
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 TryFrom for NonZeroInteger types #72712
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@rfcbot fcp merge This adds TryFrom impls to convert any integer type to any nonzero integer type. |
Team member @sfackler has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
@rfcbot concern too much or too little This PR is conflating two kinds of conversion:
It’s two different conversions chained, conceptually and even in the implementation. As far as I know there is no precedent so far for this. For example we have I think I’d prefer for this PR to stick to Or, if uses cases (please explain them!) for chained conversion in a single |
@rfcbot concern too much or too little |
Given the existence of The primary usecase is reduction in boilerplate and simplicity. The fact the only alternative, to convert all integer types into a use std::{convert::TryFrom, num::NonZeroI8};
pub fn into_nzi8<T>(x: T) -> Option<NonZeroI8>
where
i8: TryFrom<T>,
{
i8::try_from(x).ok().and_then(NonZeroI8::new)
} This is some non-trivial generic boilerplate for integer conversions! Hopefully this speaks for itself as a failure of usability/ergonomics. The added storage semantics of While the chained conversion does increase the complexity. The chain is intuitive based on existing documentation & type names. The declaration
Could you say more? If this is request for the inverse operations I can facilitate it. |
Right, but how often to you actually want to do that in the first place? I’m not totally opposed to adding these "implicitly chained" conversions if the rest of @rust-lang/libs is on board. Only this wasn’t apparent in the PR description or FCP proposal, and I feel it should be a deliberate decision.
I mean, assuming we want to go in that direction:
|
Is there anything I can do help advance this decision making process? AFAIT, the decision is now being left up to Libs-Team internal politics to determine what, if any, future actions are required. |
I think it might be best to add only the same-width conversions for now and leave the others for a later time. |
Adds
TryFrom<{integer}>
forNonZero{integer}
types.Piggybacks the existing
TryFrom<{integer}>
(to handle ranges) as well theNonZero{integer}::new
(to handle the zero check). Meaning no new logic is added in this patch. Existing logic is just chained together.