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