about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorOzaren <krishna.sd.2012@gmail.com>2018-12-18 23:18:20 -0500
committerOzaren <krishna.sd.2012@gmail.com>2018-12-18 23:18:20 -0500
commitdb9fe1c86ea3c45a459f2ff63db2aef658ee3b7f (patch)
tree5fd04778880db6e98c56608701dd53d5a907b3db /src
parenta1be81300fdc6906dc3cb19019747e622901badc (diff)
downloadrust-db9fe1c86ea3c45a459f2ff63db2aef658ee3b7f.tar.gz
rust-db9fe1c86ea3c45a459f2ff63db2aef658ee3b7f.zip
added test to show motivation for modified TryFrom impl
Diffstat (limited to 'src')
-rw-r--r--src/test/run-pass/try_from.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/test/run-pass/try_from.rs b/src/test/run-pass/try_from.rs
new file mode 100644
index 00000000000..3f2eb98f861
--- /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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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: T
+}
+
+/*
+// This fails to compile due to coherence restrictions
+// as of rust version 1.32.x
+impl<T> From<Foo<T>> for Box<T> {
+    fn from(foo: Foo<T>) -> Box<T> {
+        Box::new(foo.t)
+    }
+}
+*/
+
+impl<T> Into<Box<T>> for Foo<T> {
+    fn into(self) -> Box<T> {
+        Box::new(self.t)
+    }
+}
+
+pub fn main() {
+    let _: Result<Box<i32>, !> = Foo { t: 10 }.try_into();
+}