Skip to content

Commit db9fe1c

Browse files
committed
added test to show motivation for modified TryFrom impl
1 parent a1be813 commit db9fe1c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/test/run-pass/try_from.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test relies on `TryFrom` being auto impl for all `T: Into`
12+
// and `TryInto` being auto impl for all `U: TryFrom`
13+
14+
// This test was added to show the motivation for doing this
15+
// over `TryFrom` being auto impl for all `T: From`
16+
17+
#![feature(try_from, never_type)]
18+
19+
use std::convert::TryInto;
20+
21+
struct Foo<T> {
22+
t: T
23+
}
24+
25+
/*
26+
// This fails to compile due to coherence restrictions
27+
// as of rust version 1.32.x
28+
impl<T> From<Foo<T>> for Box<T> {
29+
fn from(foo: Foo<T>) -> Box<T> {
30+
Box::new(foo.t)
31+
}
32+
}
33+
*/
34+
35+
impl<T> Into<Box<T>> for Foo<T> {
36+
fn into(self) -> Box<T> {
37+
Box::new(self.t)
38+
}
39+
}
40+
41+
pub fn main() {
42+
let _: Result<Box<i32>, !> = Foo { t: 10 }.try_into();
43+
}

0 commit comments

Comments
 (0)