From f8bd80a830509d4356b76bc44a50c798db1bb3ea Mon Sep 17 00:00:00 2001 From: Ozaren Date: Thu, 13 Dec 2018 16:53:50 -0500 Subject: [PATCH 1/6] Change bounds on `TryFrom` blanket impl to use `Into` instead of `From` --- src/libcore/convert.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs index 2d4813718f41a..18ead0877fe85 100644 --- a/src/libcore/convert.rs +++ b/src/libcore/convert.rs @@ -476,11 +476,11 @@ impl TryInto for T where U: TryFrom // Infallible conversions are semantically equivalent to fallible conversions // with an uninhabited error type. #[unstable(feature = "try_from", issue = "33417")] -impl TryFrom for T where T: From { +impl TryFrom for T where U: Into { type Error = !; fn try_from(value: U) -> Result { - Ok(T::from(value)) + Ok(U::into(value)) } } From 7a25a7ce8df3ba8b41915e240fedd8bf94867137 Mon Sep 17 00:00:00 2001 From: Ozaren Date: Sun, 16 Dec 2018 13:01:26 -0500 Subject: [PATCH 2/6] Changed expected error message for TryFrom Reason: Due to changes in bounds of `TryFrom` from `From` to `Into` --- src/test/ui/e0119/conflict-with-std.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/e0119/conflict-with-std.stderr b/src/test/ui/e0119/conflict-with-std.stderr index e8b2c84c0df0b..8ca085c3a4bbf 100644 --- a/src/test/ui/e0119/conflict-with-std.stderr +++ b/src/test/ui/e0119/conflict-with-std.stderr @@ -25,7 +25,7 @@ LL | impl TryFrom for X { //~ ERROR conflicting implementations | = note: conflicting implementation in crate `core`: - impl std::convert::TryFrom for T - where T: std::convert::From; + where T: std::convert::Into; error: aborting due to 3 previous errors From a1be81300fdc6906dc3cb19019747e622901badc Mon Sep 17 00:00:00 2001 From: Ozaren Date: Sun, 16 Dec 2018 13:59:12 -0500 Subject: [PATCH 3/6] Fixed `Into` bound on `TryFrom` error. fix to last commit --- src/test/ui/e0119/conflict-with-std.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/e0119/conflict-with-std.stderr b/src/test/ui/e0119/conflict-with-std.stderr index 8ca085c3a4bbf..fd016941d258e 100644 --- a/src/test/ui/e0119/conflict-with-std.stderr +++ b/src/test/ui/e0119/conflict-with-std.stderr @@ -25,7 +25,7 @@ LL | impl TryFrom for X { //~ ERROR conflicting implementations | = note: conflicting implementation in crate `core`: - impl std::convert::TryFrom for T - where T: std::convert::Into; + where U: std::convert::Into; error: aborting due to 3 previous errors From db9fe1c86ea3c45a459f2ff63db2aef658ee3b7f Mon Sep 17 00:00:00 2001 From: Ozaren Date: Tue, 18 Dec 2018 23:18:20 -0500 Subject: [PATCH 4/6] added test to show motivation for modified TryFrom impl --- src/test/run-pass/try_from.rs | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/run-pass/try_from.rs diff --git a/src/test/run-pass/try_from.rs b/src/test/run-pass/try_from.rs new file mode 100644 index 0000000000000..3f2eb98f861a0 --- /dev/null +++ b/src/test/run-pass/try_from.rs @@ -0,0 +1,43 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This test relies on `TryFrom` being auto impl for all `T: Into` +// and `TryInto` being auto impl for all `U: TryFrom` + +// This test was added to show the motivation for doing this +// over `TryFrom` being auto impl for all `T: From` + +#![feature(try_from, never_type)] + +use std::convert::TryInto; + +struct Foo { + t: T +} + +/* +// This fails to compile due to coherence restrictions +// as of rust version 1.32.x +impl From> for Box { + fn from(foo: Foo) -> Box { + Box::new(foo.t) + } +} +*/ + +impl Into> for Foo { + fn into(self) -> Box { + Box::new(self.t) + } +} + +pub fn main() { + let _: Result, !> = Foo { t: 10 }.try_into(); +} From 7dd078f53f4e1817207b089fb08d6a00121ca05b Mon Sep 17 00:00:00 2001 From: Ozaren Date: Wed, 19 Dec 2018 00:19:07 -0500 Subject: [PATCH 5/6] fixed test, now it doesn't use a fundemental type i.e. `Box`, instead it now uses `Vec` --- src/test/run-pass/try_from.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/test/run-pass/try_from.rs b/src/test/run-pass/try_from.rs index 3f2eb98f861a0..767c2b9147174 100644 --- a/src/test/run-pass/try_from.rs +++ b/src/test/run-pass/try_from.rs @@ -32,12 +32,13 @@ impl From> for Box { } */ -impl Into> for Foo { - fn into(self) -> Box { - Box::new(self.t) +impl Into> for Foo { + fn into(self) -> Vec { + vec![self.t] } } pub fn main() { - let _: Result, !> = Foo { t: 10 }.try_into(); + let _: Result, !> = Foo { t: 10 }.try_into(); } + From ea68b3ff3dd5a49c5984c476570fb5404c342079 Mon Sep 17 00:00:00 2001 From: Ozaren Date: Wed, 2 Jan 2019 20:38:50 -0500 Subject: [PATCH 6/6] update to reflect changes recommended by @shepmaster his review --- src/test/run-pass/try_from.rs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/test/run-pass/try_from.rs b/src/test/run-pass/try_from.rs index 767c2b9147174..4522ce3a8d617 100644 --- a/src/test/run-pass/try_from.rs +++ b/src/test/run-pass/try_from.rs @@ -1,30 +1,23 @@ -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// This test relies on `TryFrom` being auto impl for all `T: Into` -// and `TryInto` being auto impl for all `U: TryFrom` +// 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 auto impl for all `T: From` +// over `TryFrom` being blanket impl for all `T: From` #![feature(try_from, never_type)] use std::convert::TryInto; struct Foo { - t: T + t: T, } -/* // This fails to compile due to coherence restrictions -// as of rust version 1.32.x +// 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 From> for Box { fn from(foo: Foo) -> Box { Box::new(foo.t)