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