-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #49305 - SimonSapin:fallible, r=sfackler
Stabilize TryFrom / TryInto, and tweak impls for integers 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 generic `impl<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 define `type Error = !` all involve `usize` or `isize`. This PR changes them to use `TryFromIntError` 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 only `impl From<usize> for u128` and `impl From<isize> for u128`. I [found](https://www.cl.cam.ac.uk/research/security/ctsrd/pdfs/20171017a-cheri-poster.pdf) some [things](http://www.csl.sri.com/users/neumann/2012resolve-cheri.pdf) about 256-bit “capabilities”, but I don’t know how relevant that would be to Rust’s `usize` and `isize` 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: * The `convert::TryFrom` trait * The `convert::TryFrom` trait * `impl<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) * The `array::TryFromSliceError` struct, with impls of `Debug`, `Copy`, `Clone`, and `Error` * `impl TryFrom<u32> for char` * The `char::CharTryFromError` struct, with impls of `Copy`, `Clone`, `Debug`, `PartialEq`, `Eq`, `Display`, and `Error` * Impls of `TryFrom` for all (?) combinations of primitive integer types where `From` isn’t implemented. * The `num::TryFromIntError` struct, with impls of `Debug`, `Copy`, `Clone`, `Display`, `From<!>`, and `Error` Some minor remaining questions that I hope can be resolved in this PR: * Should the impls for error types be unified? * ~Should `TryFrom` and `TryInto` be in the prelude? `From` and `Into` are.~ (Yes.)
- Loading branch information
Showing
17 changed files
with
147 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.