]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/try_from.rs
Rollup merge of #61499 - varkor:issue-53457, r=oli-obk
[rust.git] / src / test / run-pass / try_from.rs
1 // This test relies on `TryFrom` being blanket impl for all `T: Into`
2 // and `TryInto` being blanket impl for all `U: TryFrom`
3
4 // This test was added to show the motivation for doing this
5 // over `TryFrom` being blanket impl for all `T: From`
6
7 #![feature(never_type)]
8
9 use std::convert::{TryInto, Infallible};
10
11 struct Foo<T> {
12     t: T,
13 }
14
15 // This fails to compile due to coherence restrictions
16 // as of Rust version 1.32.x, therefore it could not be used
17 // instead of the `Into` version of the impl, and serves as
18 // motivation for a blanket impl for all `T: Into`, instead
19 // of a blanket impl for all `T: From`
20 /*
21 impl<T> From<Foo<T>> for Box<T> {
22     fn from(foo: Foo<T>) -> Box<T> {
23         Box::new(foo.t)
24     }
25 }
26 */
27
28 impl<T> Into<Vec<T>> for Foo<T> {
29     fn into(self) -> Vec<T> {
30         vec![self.t]
31     }
32 }
33
34 pub fn main() {
35     let _: Result<Vec<i32>, Infallible> = Foo { t: 10 }.try_into();
36 }