Skip to content

Commit

Permalink
Stabilize TryFrom and TryInto
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Feb 13, 2019
1 parent 2f71203 commit c80a8f5
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/libcore/array.rs
Expand Up @@ -49,7 +49,7 @@ unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
} }


/// The error type returned when a conversion from a slice to an array fails. /// The error type returned when a conversion from a slice to an array fails.
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct TryFromSliceError(()); pub struct TryFromSliceError(());


Expand Down Expand Up @@ -138,7 +138,7 @@ macro_rules! array_impls {
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl<'a, T> TryFrom<&'a [T]> for [T; $N] where T: Copy { impl<'a, T> TryFrom<&'a [T]> for [T; $N] where T: Copy {
type Error = TryFromSliceError; type Error = TryFromSliceError;


Expand All @@ -147,7 +147,7 @@ macro_rules! array_impls {
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] { impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] {
type Error = TryFromSliceError; type Error = TryFromSliceError;


Expand All @@ -161,7 +161,7 @@ macro_rules! array_impls {
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] { impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] {
type Error = TryFromSliceError; type Error = TryFromSliceError;


Expand Down
6 changes: 3 additions & 3 deletions src/libcore/char/convert.rs
Expand Up @@ -218,7 +218,7 @@ impl FromStr for char {
} }




#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<u32> for char { impl TryFrom<u32> for char {
type Error = CharTryFromError; type Error = CharTryFromError;


Expand All @@ -233,11 +233,11 @@ impl TryFrom<u32> for char {
} }


/// The error type returned when a conversion from u32 to char fails. /// The error type returned when a conversion from u32 to char fails.
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct CharTryFromError(()); pub struct CharTryFromError(());


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl fmt::Display for CharTryFromError { impl fmt::Display for CharTryFromError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"converted integer out of range for `char`".fmt(f) "converted integer out of range for `char`".fmt(f)
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/char/mod.rs
Expand Up @@ -30,7 +30,7 @@ pub use self::convert::{from_u32, from_digit};
pub use self::convert::from_u32_unchecked; pub use self::convert::from_u32_unchecked;
#[stable(feature = "char_from_str", since = "1.20.0")] #[stable(feature = "char_from_str", since = "1.20.0")]
pub use self::convert::ParseCharError; pub use self::convert::ParseCharError;
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
pub use self::convert::CharTryFromError; pub use self::convert::CharTryFromError;
#[stable(feature = "decode_utf16", since = "1.9.0")] #[stable(feature = "decode_utf16", since = "1.9.0")]
pub use self::decode::{decode_utf16, DecodeUtf16, DecodeUtf16Error}; pub use self::decode::{decode_utf16, DecodeUtf16, DecodeUtf16Error};
Expand Down
12 changes: 8 additions & 4 deletions src/libcore/convert.rs
Expand Up @@ -370,22 +370,26 @@ pub trait From<T>: Sized {
/// ///
/// [`TryFrom`]: trait.TryFrom.html /// [`TryFrom`]: trait.TryFrom.html
/// [`Into`]: trait.Into.html /// [`Into`]: trait.Into.html
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
pub trait TryInto<T>: Sized { pub trait TryInto<T>: Sized {
/// The type returned in the event of a conversion error. /// The type returned in the event of a conversion error.
#[stable(feature = "try_from", since = "1.34.0")]
type Error; type Error;


/// Performs the conversion. /// Performs the conversion.
#[stable(feature = "try_from", since = "1.34.0")]
fn try_into(self) -> Result<T, Self::Error>; fn try_into(self) -> Result<T, Self::Error>;
} }


/// Attempt to construct `Self` via a conversion. /// Attempt to construct `Self` via a conversion.
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
pub trait TryFrom<T>: Sized { pub trait TryFrom<T>: Sized {
/// The type returned in the event of a conversion error. /// The type returned in the event of a conversion error.
#[stable(feature = "try_from", since = "1.34.0")]
type Error; type Error;


/// Performs the conversion. /// Performs the conversion.
#[stable(feature = "try_from", since = "1.34.0")]
fn try_from(value: T) -> Result<Self, Self::Error>; fn try_from(value: T) -> Result<Self, Self::Error>;
} }


Expand Down Expand Up @@ -453,7 +457,7 @@ impl<T> From<T> for T {




// TryFrom implies TryInto // TryFrom implies TryInto
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl<T, U> TryInto<U> for T where U: TryFrom<T> impl<T, U> TryInto<U> for T where U: TryFrom<T>
{ {
type Error = U::Error; type Error = U::Error;
Expand All @@ -465,7 +469,7 @@ impl<T, U> TryInto<U> for T where U: TryFrom<T>


// Infallible conversions are semantically equivalent to fallible conversions // Infallible conversions are semantically equivalent to fallible conversions
// with an uninhabited error type. // with an uninhabited error type.
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl<T, U> TryFrom<U> for T where U: Into<T> { impl<T, U> TryFrom<U> for T where U: Into<T> {
type Error = Infallible; type Error = Infallible;


Expand Down
20 changes: 7 additions & 13 deletions src/libcore/num/mod.rs
Expand Up @@ -2004,7 +2004,6 @@ assert_eq!(value, ", $swap_op, ");
When starting from a slice rather than an array, fallible conversion APIs can be used: When starting from a slice rather than an array, fallible conversion APIs can be used:
``` ```
#![feature(try_from)]
use std::convert::TryInto; use std::convert::TryInto;
fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
Expand Down Expand Up @@ -2036,7 +2035,6 @@ assert_eq!(value, ", $swap_op, ");
When starting from a slice rather than an array, fallible conversion APIs can be used: When starting from a slice rather than an array, fallible conversion APIs can be used:
``` ```
#![feature(try_from)]
use std::convert::TryInto; use std::convert::TryInto;
fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
Expand Down Expand Up @@ -2078,7 +2076,6 @@ assert_eq!(value, ", $swap_op, ");
When starting from a slice rather than an array, fallible conversion APIs can be used: When starting from a slice rather than an array, fallible conversion APIs can be used:
``` ```
#![feature(try_from)]
use std::convert::TryInto; use std::convert::TryInto;
fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
Expand Down Expand Up @@ -3771,7 +3768,6 @@ assert_eq!(value, ", $swap_op, ");
When starting from a slice rather than an array, fallible conversion APIs can be used: When starting from a slice rather than an array, fallible conversion APIs can be used:
``` ```
#![feature(try_from)]
use std::convert::TryInto; use std::convert::TryInto;
fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
Expand Down Expand Up @@ -3803,7 +3799,6 @@ assert_eq!(value, ", $swap_op, ");
When starting from a slice rather than an array, fallible conversion APIs can be used: When starting from a slice rather than an array, fallible conversion APIs can be used:
``` ```
#![feature(try_from)]
use std::convert::TryInto; use std::convert::TryInto;
fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
Expand Down Expand Up @@ -3845,7 +3840,6 @@ assert_eq!(value, ", $swap_op, ");
When starting from a slice rather than an array, fallible conversion APIs can be used: When starting from a slice rather than an array, fallible conversion APIs can be used:
``` ```
#![feature(try_from)]
use std::convert::TryInto; use std::convert::TryInto;
fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {
Expand Down Expand Up @@ -4508,7 +4502,7 @@ macro_rules! from_str_radix_int_impl {
from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 } from_str_radix_int_impl! { isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 }


/// The error type returned when a checked integral type conversion fails. /// The error type returned when a checked integral type conversion fails.
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TryFromIntError(()); pub struct TryFromIntError(());


Expand All @@ -4523,14 +4517,14 @@ impl TryFromIntError {
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl fmt::Display for TryFromIntError { impl fmt::Display for TryFromIntError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(fmt) self.__description().fmt(fmt)
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl From<Infallible> for TryFromIntError { impl From<Infallible> for TryFromIntError {
fn from(x: Infallible) -> TryFromIntError { fn from(x: Infallible) -> TryFromIntError {
match x {} match x {}
Expand All @@ -4550,7 +4544,7 @@ impl From<!> for TryFromIntError {
// no possible bounds violation // no possible bounds violation
macro_rules! try_from_unbounded { macro_rules! try_from_unbounded {
($source:ty, $($target:ty),*) => {$( ($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target { impl TryFrom<$source> for $target {
type Error = TryFromIntError; type Error = TryFromIntError;


Expand All @@ -4565,7 +4559,7 @@ macro_rules! try_from_unbounded {
// only negative bounds // only negative bounds
macro_rules! try_from_lower_bounded { macro_rules! try_from_lower_bounded {
($source:ty, $($target:ty),*) => {$( ($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target { impl TryFrom<$source> for $target {
type Error = TryFromIntError; type Error = TryFromIntError;


Expand All @@ -4584,7 +4578,7 @@ macro_rules! try_from_lower_bounded {
// unsigned to signed (only positive bound) // unsigned to signed (only positive bound)
macro_rules! try_from_upper_bounded { macro_rules! try_from_upper_bounded {
($source:ty, $($target:ty),*) => {$( ($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target { impl TryFrom<$source> for $target {
type Error = TryFromIntError; type Error = TryFromIntError;


Expand All @@ -4603,7 +4597,7 @@ macro_rules! try_from_upper_bounded {
// all other cases // all other cases
macro_rules! try_from_both_bounded { macro_rules! try_from_both_bounded {
($source:ty, $($target:ty),*) => {$( ($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target { impl TryFrom<$source> for $target {
type Error = TryFromIntError; type Error = TryFromIntError;


Expand Down
1 change: 0 additions & 1 deletion src/libcore/tests/lib.rs
Expand Up @@ -27,7 +27,6 @@
#![feature(str_internals)] #![feature(str_internals)]
#![feature(test)] #![feature(test)]
#![feature(trusted_len)] #![feature(trusted_len)]
#![feature(try_from)]
#![feature(try_trait)] #![feature(try_trait)]
#![feature(align_offset)] #![feature(align_offset)]
#![feature(reverse_bits)] #![feature(reverse_bits)]
Expand Down
1 change: 0 additions & 1 deletion src/librustc_apfloat/lib.rs
Expand Up @@ -35,7 +35,6 @@
#![deny(rust_2018_idioms)] #![deny(rust_2018_idioms)]


#![feature(nll)] #![feature(nll)]
#![feature(try_from)]
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this. // See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
#[allow(unused_extern_crates)] #[allow(unused_extern_crates)]
extern crate rustc_cratesio_shim; extern crate rustc_cratesio_shim;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/lib.rs
Expand Up @@ -24,7 +24,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
#![feature(unicode_internals)] #![feature(unicode_internals)]
#![feature(step_trait)] #![feature(step_trait)]
#![feature(slice_concat_ext)] #![feature(slice_concat_ext)]
#![feature(try_from)]
#![feature(reverse_bits)] #![feature(reverse_bits)]
#![feature(try_blocks)] #![feature(try_blocks)]


Expand Down
6 changes: 3 additions & 3 deletions src/libstd/error.rs
Expand Up @@ -466,14 +466,14 @@ impl Error for num::ParseIntError {
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl Error for num::TryFromIntError { impl Error for num::TryFromIntError {
fn description(&self) -> &str { fn description(&self) -> &str {
self.__description() self.__description()
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl Error for array::TryFromSliceError { impl Error for array::TryFromSliceError {
fn description(&self) -> &str { fn description(&self) -> &str {
self.__description() self.__description()
Expand Down Expand Up @@ -548,7 +548,7 @@ impl Error for cell::BorrowMutError {
} }
} }


#[unstable(feature = "try_from", issue = "33417")] #[stable(feature = "try_from", since = "1.34.0")]
impl Error for char::CharTryFromError { impl Error for char::CharTryFromError {
fn description(&self) -> &str { fn description(&self) -> &str {
"converted integer out of range for `char`" "converted integer out of range for `char`"
Expand Down
1 change: 0 additions & 1 deletion src/libstd/lib.rs
Expand Up @@ -281,7 +281,6 @@
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(thread_local)] #![feature(thread_local)]
#![feature(toowned_clone_into)] #![feature(toowned_clone_into)]
#![feature(try_from)]
#![feature(try_reserve)] #![feature(try_reserve)]
#![feature(unboxed_closures)] #![feature(unboxed_closures)]
#![feature(untagged_unions)] #![feature(untagged_unions)]
Expand Down
1 change: 0 additions & 1 deletion src/test/run-pass/try-from-int-error-partial-eq.rs
@@ -1,4 +1,3 @@
#![feature(try_from)]
#![allow(unused_must_use)] #![allow(unused_must_use)]


use std::convert::TryFrom; use std::convert::TryFrom;
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/try_from.rs
Expand Up @@ -4,7 +4,7 @@
// This test was added to show the motivation for doing this // This test was added to show the motivation for doing this
// over `TryFrom` being blanket impl for all `T: From` // over `TryFrom` being blanket impl for all `T: From`


#![feature(try_from, never_type)] #![feature(never_type)]


use std::convert::{TryInto, Infallible}; use std::convert::{TryInto, Infallible};


Expand Down
1 change: 0 additions & 1 deletion src/test/ui/e0119/conflict-with-std.rs
@@ -1,4 +1,3 @@
#![feature(try_from)]


use std::marker::PhantomData; use std::marker::PhantomData;
use std::convert::{TryFrom, AsRef}; use std::convert::{TryFrom, AsRef};
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/e0119/conflict-with-std.stderr
@@ -1,5 +1,5 @@
error[E0119]: conflicting implementations of trait `std::convert::AsRef<Q>` for type `std::boxed::Box<Q>`: error[E0119]: conflicting implementations of trait `std::convert::AsRef<Q>` for type `std::boxed::Box<Q>`:
--> $DIR/conflict-with-std.rs:7:1 --> $DIR/conflict-with-std.rs:6:1
| |
LL | impl AsRef<Q> for Box<Q> { //~ ERROR conflicting implementations LL | impl AsRef<Q> for Box<Q> { //~ ERROR conflicting implementations
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -9,7 +9,7 @@ LL | impl AsRef<Q> for Box<Q> { //~ ERROR conflicting implementations
where T: ?Sized; where T: ?Sized;


error[E0119]: conflicting implementations of trait `std::convert::From<S>` for type `S`: error[E0119]: conflicting implementations of trait `std::convert::From<S>` for type `S`:
--> $DIR/conflict-with-std.rs:14:1 --> $DIR/conflict-with-std.rs:13:1
| |
LL | impl From<S> for S { //~ ERROR conflicting implementations LL | impl From<S> for S { //~ ERROR conflicting implementations
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
Expand All @@ -18,7 +18,7 @@ LL | impl From<S> for S { //~ ERROR conflicting implementations
- impl<T> std::convert::From<T> for T; - impl<T> std::convert::From<T> for T;


error[E0119]: conflicting implementations of trait `std::convert::TryFrom<X>` for type `X`: error[E0119]: conflicting implementations of trait `std::convert::TryFrom<X>` for type `X`:
--> $DIR/conflict-with-std.rs:21:1 --> $DIR/conflict-with-std.rs:20:1
| |
LL | impl TryFrom<X> for X { //~ ERROR conflicting implementations LL | impl TryFrom<X> for X { //~ ERROR conflicting implementations
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit c80a8f5

Please sign in to comment.