Skip to content

Commit

Permalink
added test to show motivation for modified TryFrom impl
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyYato committed Dec 19, 2018
1 parent a1be813 commit db9fe1c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 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();
}

0 comments on commit db9fe1c

Please sign in to comment.