3 #![feature(control_flow_enum)]
4 #![feature(try_trait_v2)]
6 use std::ops::{ControlFlow, FromResidual, Try};
15 impl<U, V> Try for MyResult<U, V> {
17 type Residual = MyResult<Never, V>;
19 fn from_output(u: U) -> MyResult<U, V> {
23 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
25 MyResult::Awesome(u) => ControlFlow::Continue(u),
26 MyResult::Terrible(e) => ControlFlow::Break(MyResult::Terrible(e)),
31 impl<U, V, W> FromResidual<MyResult<Never, V>> for MyResult<U, W> where V: Into<W> {
32 fn from_residual(x: MyResult<Never, V>) -> Self {
34 MyResult::Awesome(u) => match u {},
35 MyResult::Terrible(e) => MyResult::Terrible(e.into()),
40 type ResultResidual<E> = Result<std::convert::Infallible, E>;
42 impl<U, V, W> FromResidual<ResultResidual<V>> for MyResult<U, W> where V: Into<W> {
43 fn from_residual(x: ResultResidual<V>) -> Self {
46 Err(e) => MyResult::Terrible(e.into()),
51 impl<U, V, W> FromResidual<MyResult<Never, V>> for Result<U, W> where V: Into<W> {
52 fn from_residual(x: MyResult<Never, V>) -> Self {
54 MyResult::Awesome(u) => match u {},
55 MyResult::Terrible(e) => Err(e.into()),
60 fn f(x: i32) -> Result<i32, String> {
69 fn g(x: i32) -> MyResult<i32, String> {
71 MyResult::Terrible("Hello".to_owned())
74 fn h() -> MyResult<i32, String> {
75 let a: Result<i32, &'static str> = Err("Hello");
80 fn i() -> MyResult<i32, String> {
81 let a: MyResult<i32, &'static str> = MyResult::Terrible("Hello");
87 assert!(f(0) == Ok(42));
88 assert!(f(10) == Err("Hello".to_owned()));