| 1 |
// { dg-additional-options "-frust-compile-until=typecheck" } |
| 2 |
|
| 3 |
#[lang = "sized"] |
| 4 |
trait Sized {} |
| 5 |
|
| 6 |
enum Result { |
| 7 |
#[lang = "Ok"] |
| 8 |
Ok(i32), |
| 9 |
#[lang = "Err"] |
| 10 |
Err(i32) |
| 11 |
} |
| 12 |
|
| 13 |
pub trait From<T>: Sized { |
| 14 |
/// Performs the conversion. |
| 15 |
#[lang = "from"] |
| 16 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 17 |
fn from(_: T) -> Self; |
| 18 |
} |
| 19 |
|
| 20 |
impl<T> From<T> for T { |
| 21 |
fn from(t: T) -> Self { t } |
| 22 |
} |
| 23 |
|
| 24 |
#[lang = "try"] |
| 25 |
pub trait Try { |
| 26 |
/// The type of this value when viewed as successful. |
| 27 |
// #[unstable(feature = "try_trait", issue = "42327")] |
| 28 |
// type Ok; |
| 29 |
/// The type of this value when viewed as failed. |
| 30 |
// #[unstable(feature = "try_trait", issue = "42327")] |
| 31 |
// type Error; |
| 32 |
|
| 33 |
/// Applies the "?" operator. A return of `Ok(t)` means that the |
| 34 |
/// execution should continue normally, and the result of `?` is the |
| 35 |
/// value `t`. A return of `Err(e)` means that execution should branch |
| 36 |
/// to the innermost enclosing `catch`, or return from the function. |
| 37 |
/// |
| 38 |
/// If an `Err(e)` result is returned, the value `e` will be "wrapped" |
| 39 |
/// in the return type of the enclosing scope (which must itself implement |
| 40 |
/// `Try`). Specifically, the value `X::from_error(From::from(e))` |
| 41 |
/// is returned, where `X` is the return type of the enclosing function. |
| 42 |
#[lang = "into_result"] |
| 43 |
#[unstable(feature = "try_trait", issue = "42327")] |
| 44 |
fn into_result(self) -> Result; |
| 45 |
|
| 46 |
/// Wrap an error value to construct the composite result. For example, |
| 47 |
/// `Result::Err(x)` and `Result::from_error(x)` are equivalent. |
| 48 |
#[lang = "from_error"] |
| 49 |
#[unstable(feature = "try_trait", issue = "42327")] |
| 50 |
fn from_error(v: i32) -> Self; |
| 51 |
|
| 52 |
/// Wrap an OK value to construct the composite result. For example, |
| 53 |
/// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent. |
| 54 |
#[lang = "from_ok"] |
| 55 |
#[unstable(feature = "try_trait", issue = "42327")] |
| 56 |
fn from_ok(v: i32) -> Self; |
| 57 |
} |
| 58 |
|
| 59 |
impl Try for Result { |
| 60 |
// type Ok = i32; |
| 61 |
// type Error = i32; |
| 62 |
|
| 63 |
fn into_result(self) -> Result { |
| 64 |
self |
| 65 |
} |
| 66 |
|
| 67 |
fn from_ok(v: i32) -> Self { |
| 68 |
Result::Ok(v) |
| 69 |
} |
| 70 |
|
| 71 |
fn from_error(v: i32) -> Self { |
| 72 |
Result::Err(v) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
fn bar() -> Result { |
| 77 |
Result::Ok(15) |
| 78 |
} |
| 79 |
|
| 80 |
fn foo() -> Result { |
| 81 |
let a = bar()?; |
| 82 |
|
| 83 |
Result::Ok(a) |
| 84 |
} |