-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Stabilize TryFrom / TryInto, and tweak impls for integers #49305
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (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. |
I think they should, so I added a commit that makes them so. Let’s see if anyone objects :) |
We already did the FCP dance in the tracking issue, but just to get eyes on this again: @rfcbot fcp merge |
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), 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. |
The problem with this is that when (and I hope it's a when) the missing |
@ollie27 Which impls are you saying are missing? After this PR I believe that that every impl that can be implemented in on all platforms between primitive integers are already implemented. There shouldn’t be any we haven’t gotten around to yet. As to type aliases like |
All of the impls that aren't strictly speaking portable. They were proposed in #37423 but postponed for the portability lint.
Exactly. I don't see why people can't do this for |
As far as I can tell the portability lint wouldn’t help much here. We want
Just because a work around exists doesn’t mean it’s not bad. This PR tries to avoid this situation where we can. |
Just to be clear, are you saying that the impls proposed in #37423 will never be added? If you are then I strongly oppose that. Having |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
With this PR as originally proposed, indeed those could never be added as they would conflict with impls being stabilized here. When discussing this offline @sfackler suggested that with the portability lint we could have some The set of impls that would be affected is not obvious so I tried to make an exhaustive table (with the same assumptions about pointer width than in this PR’s original message: at least 16 bits, but no assumed upper bound)
Legend:
Whatever else we do, the few I think there are four things we can do with the rest:
I’m now leaning toward 3 or 4. |
I think 4 may be the cleanest but 3 also seems fine. |
I’ve implemented 3 since that seems more useful. Most users don’t need to figure out mentally which impls exist or not, they can just try to use it and see what happens. I’ve had to modify range iterators and r? @sfackler |
☔ The latest upstream changes (presumably #49101) made this pull request unmergeable. Please resolve the merge conflicts. |
To add to this Build : https://travis-ci.org/rust-lang-nursery/rand/jobs/359738013 |
I downloaded the modules metadata from https://github.com/rust-lang/crates.io-index and inserted them into MongoDB to analyze the number of modules that might break. I did a query for stdweb and http. I also did a query on the number of packages that depend on rand which uses stdweb. The approx results are as below : stdweb - 15 dependants Gist of dependencies : https://gist.github.com/tirkarthi/846f9d2fd87c90463c5ca9cf99f3d44d |
FYI, @SimonSapin opened a PR which reverts the |
Revert "Add TryFrom and TryInto to the prelude" This reverts commit 09008cc. This addition landed in #49305 and turned out to break crates that had their own copy of `TryFrom` in order to use it on the Stable channel :( We’ll explore the possibility of the 2018 edition having a different prelude that includes this traits. However per the editions RFC this requires implementing a warning in the 2015 edition for code that *would* break.
Oops, I meant to comment about this here but forgot. Thanks @frewsxcv. As mentioned in that PR I’d still like to figure something out later, but for now let’s fix immediate breakage. |
Because rust-lang/rust#49305 removed the TryFrom conversions from usize (temporarily), we have to use a fixed version of rustc. It's unclear when we'll be able to revert this :/
How does one handle something like libc::time_t using the new TryFrom? |
@SoniEx2 A merged or otherwise closed PR/issue is often not a good place to ask question. Consider asking on #33417 or on https://internals.rust-lang.org/ instead. But also: what do you mean by "handle"? Convert to/from what other type? |
say I need to turn a time_t to/from u64. since time_t can change types, this seems like I'd have to be putting either "as" or #[cfg] everywhere? none of them let me write code that "just works"... I wonder why we couldn't have multiple otherwise identical TryFroms with different error types... |
It looks like Note that infallible conversions still get a fn foo(x: u64) -> Result<time_t, TryFromIntError> {
Ok(x.try_into()?)
} This works because |
Fixes #33417 (tracking issue)
This adds:
impl From<u16> for usize
impl From<i16> for isize
impl From<u8> for isize
… replacing corresponding
TryFrom<Error=!>
impls. (TryFrom
still applies through the genericimpl<T, U> TryFrom<U> for T where T: From<U>
.) Their infallibility is supported by the C99 standard which (indirectly) requires pointers to be at least 16 bits.The remaining
TryFrom
impls that definetype Error = !
all involveusize
orisize
. This PR changes them to useTryFromIntError
instead, since having a return type change based on the target is a portability hazard.Note: if we make similar assumptions about the maximum bit size of pointers (for all targets Rust will ever run on in the future), we could have similar
From
impls converting pointer-sized integers to large fixed-size integers. RISC-V considers the possibility of a 128-bit address space (RV128), which would leave onlyimpl From<usize> for u128
andimpl From<isize> for u128
. I found some things about 256-bit “capabilities”, but I don’t know how relevant that would be to Rust’susize
andisize
types.I chose conservatively to make no assumption about the future there. Users making their portability decisions and using something like
.try_into().unwrap()
.Since this feature already went through FCP in the tracking issue #33417, this PR also proposes stabilize the following items:
convert::TryFrom
traitconvert::TryFrom
traitimpl<T> TryFrom<&[T]> for &[T; $N]
(for$N
up to 32)impl<T> TryFrom<&mut [T]> for &mut [T; $N]
(for$N
up to 32)array::TryFromSliceError
struct, with impls ofDebug
,Copy
,Clone
, andError
impl TryFrom<u32> for char
char::CharTryFromError
struct, with impls ofCopy
,Clone
,Debug
,PartialEq
,Eq
,Display
, andError
TryFrom
for all (?) combinations of primitive integer types whereFrom
isn’t implemented.num::TryFromIntError
struct, with impls ofDebug
,Copy
,Clone
,Display
,From<!>
, andError
Some minor remaining questions that I hope can be resolved in this PR:
Should(Yes.)TryFrom
andTryInto
be in the prelude?From
andInto
are.