Rollup merge of #56796 - KrishnaSannasi:try_from_impl_change, r=shepmaster

Change bounds on `TryFrom` blanket impl to use `Into` instead of `From`

This is from this [comment](https://github.com/rust-lang/rust/issues/33417#issuecomment-447111156) I made.

This will expand the impls available for `TryFrom` and `TryInto`, without losing anything in the process.
This commit is contained in:
Mazdak Farrokhzad 2019-01-21 02:21:53 +01:00 committed by GitHub
commit ebc70e2e9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 3 deletions

View file

@ -0,0 +1,37 @@
// This test relies on `TryFrom` being blanket impl for all `T: Into`
// and `TryInto` being blanket impl for all `U: TryFrom`
// This test was added to show the motivation for doing this
// over `TryFrom` being blanket impl for all `T: From`
#![feature(try_from, never_type)]
use std::convert::TryInto;
struct Foo<T> {
t: T,
}
// This fails to compile due to coherence restrictions
// as of Rust version 1.32.x, therefore it could not be used
// instead of the `Into` version of the impl, and serves as
// motivation for a blanket impl for all `T: Into`, instead
// of a blanket impl for all `T: From`
/*
impl<T> From<Foo<T>> for Box<T> {
fn from(foo: Foo<T>) -> Box<T> {
Box::new(foo.t)
}
}
*/
impl<T> Into<Vec<T>> for Foo<T> {
fn into(self) -> Vec<T> {
vec![self.t]
}
}
pub fn main() {
let _: Result<Vec<i32>, !> = Foo { t: 10 }.try_into();
}

View file

@ -25,7 +25,7 @@ LL | impl TryFrom<X> for X { //~ ERROR conflicting implementations
|
= note: conflicting implementation in crate `core`:
- impl<T, U> std::convert::TryFrom<U> for T
where T: std::convert::From<U>;
where U: std::convert::Into<T>;
error: aborting due to 3 previous errors