Use convert::Infallible instead of never in the blanket TryFrom impl

This commit is contained in:
Simon Sapin 2019-02-08 14:55:44 +01:00
parent 85f13f0d42
commit 2f7120397f
3 changed files with 15 additions and 5 deletions

View File

@ -467,7 +467,7 @@ impl<T, U> TryInto<U> for T where U: TryFrom<T>
// with an uninhabited error type.
#[unstable(feature = "try_from", issue = "33417")]
impl<T, U> TryFrom<U> for T where U: Into<T> {
type Error = !;
type Error = Infallible;
fn try_from(value: U) -> Result<Self, Self::Error> {
Ok(U::into(value))

View File

@ -2,7 +2,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
use convert::TryFrom;
use convert::{TryFrom, Infallible};
use fmt;
use intrinsics;
use mem;
@ -4531,9 +4531,19 @@ impl fmt::Display for TryFromIntError {
}
#[unstable(feature = "try_from", issue = "33417")]
impl From<Infallible> for TryFromIntError {
fn from(x: Infallible) -> TryFromIntError {
match x {}
}
}
#[unstable(feature = "never_type", issue = "35121")]
impl From<!> for TryFromIntError {
fn from(never: !) -> TryFromIntError {
never
// Match rather than coerce to make sure that code like
// `From<Infallible> for TryFromIntError` above will keep working
// when `Infallible` becomes an alias to `!`.
match never {}
}
}

View File

@ -6,7 +6,7 @@
#![feature(try_from, never_type)]
use std::convert::TryInto;
use std::convert::{TryInto, Infallible};
struct Foo<T> {
t: T,
@ -32,6 +32,6 @@ impl<T> Into<Vec<T>> for Foo<T> {
}
pub fn main() {
let _: Result<Vec<i32>, !> = Foo { t: 10 }.try_into();
let _: Result<Vec<i32>, Infallible> = Foo { t: 10 }.try_into();
}