-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add non-portable infallible conversion traits for usize/isize #71360
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
Conversation
This follows the precedent of the `std::os` module for handling (non-)portablity. See the doc-comment of the new module for what problem this is solving and how.
r? @shepmaster (rust_highfive has picked a reviewer for you, use r? to override) |
This is an alternative to #70460 following the precedent of Details to be resolved:
Rendered copy of the doc-comment:
|
@rust-lang/rustdoc In what cases does rustdoc render |
How about |
This functionality does not fit the description in the current doc-comment for |
You'll need to also add a matching |
Sorry, wanted to hit the "comment" button and failed badly... About the doc thing, just like @ollie27 said. |
One downside, or perhaps positive side, of this approach is that -- because it introduces a different trait -- it means that generic code based on This means that if I have some function like fn foo<T: Into<u64>>() { .. } or whatever, I can't use it with On the other hand, it may be that in this particular case, composability probably comes up less often (though I'm not sure). Further, I imagine that there are cases where the conversion from |
Having to manually import a (long) trait and then use I can see several ways to ease this burden:
|
Separate traits mostly defeat the purpose in this case, IMO. |
The name could be shorter, for example If Having these traits in the prelude would defeat the point of making import from
I don’t think there is consensus or precedent for the "mostly" part. |
I'm somewhat surprised by this. Wasn't that indeed the point of the portability RFC? I guess you're saying that, in the time since, consensus has eroded, or circumstances have changed? I personally still think it makes sense to have a notion of 'mostly portable' and to make code targeting 'common platforms' ergonomic and easy. I think that trying to encourage "too much" portability is frustrating and likely to lead people to stop carrying altogether: but making it easy and painless to be portable amongst 32-bit/64-bit and win/linux/mac etc is something most people are happy to do. |
To me, this is the biggest win. Just this last week, I wrote this code: let expected_len_us = usize::try_from(expected_len)
.expect("Only designed to run on 32-bit systems or higher"); I'd much rather that the person compiling the code for a 16-bit platform get a compilation error than some poor end user of the server get a runtime panic. |
moving this to waiting-on-team as team consensus seems to be needed here in order to proceed |
I am not particularly fond of this solution. As @Amanieu said, using these traits is quite a hassle and most users will just use Without having read all the discussion about this topic, the initial PR seemed like quite a nice solution to me. It works with the standard Maybe I missed it, but have you explained what you think is the advantage of your proposal over the original PR? |
@SimonSapin any updates on this? |
I’m closing this PR since it’s not clear to me what next step could be to go toward consensus, and I don’t have bandwidth to pursue this at the moment. Anyone should feel free to pick it up again (or propose something different). |
This follows the precedent of the
std::os
module for handling (non-)portablity.See the doc-comment of the new module for what problem this is solving and how.