1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::task::Poll;

/// A stable version of [`core::ops::Try`].

pub trait Try {
    /// The type of this value when viewed as successful.

    type Ok;
    /// The type of this value when viewed as failed.

    type Error;

    /// A return of `Ok(t)` means that the

    /// execution should continue normally, and the result of `?` is the

    /// value `t`. A return of `Err(e)` means that execution should branch

    /// to the innermost enclosing `catch`, or return from the function.

    fn into_result(self) -> Result<Self::Ok, Self::Error>;

    /// Wrap an error value to construct the composite result. For example,

    /// `Result::Err(x)` and `Result::from_error(x)` are equivalent.

    fn from_error(v: Self::Error) -> Self;

    /// Wrap an OK value to construct the composite result. For example,

    /// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.

    fn from_ok(v: Self::Ok) -> Self;
}

impl<T, E> Try for Result<T, E> {
    type Ok = T;
    type Error = E;

    fn into_result(self) -> Result<<Self as Try>::Ok, <Self as Try>::Error> {
        self
    }
    fn from_error(v: <Self as Try>::Error) -> Self {
        Err(v)
    }
    fn from_ok(v: <Self as Try>::Ok) -> Self {
        Ok(v)
    }
}

/// The error type that results from applying the try operator (`?`) to a `None` value.

pub struct NoneError;

impl<T> Try for Option<T> {
    type Ok = T;
    type Error = NoneError;

    fn into_result(self) -> Result<<Self as Try>::Ok, <Self as Try>::Error> {
        self.ok_or(NoneError)
    }
    fn from_error(_v: <Self as Try>::Error) -> Self {
        None
    }
    fn from_ok(v: <Self as Try>::Ok) -> Self {
        Some(v)
    }
}

impl<T, E> Try for Poll<Option<Result<T, E>>> {
    type Ok = Poll<Option<T>>;
    type Error = E;

    fn into_result(self) -> Result<<Self as Try>::Ok, <Self as Try>::Error> {
        match self {
            Poll::Ready(Some(Ok(x))) => Ok(Poll::Ready(Some(x))),
            Poll::Ready(Some(Err(e))) => Err(e),
            Poll::Ready(None) => Ok(Poll::Ready(None)),
            Poll::Pending => Ok(Poll::Pending),
        }
    }

    fn from_error(v: <Self as Try>::Error) -> Self {
        Poll::Ready(Some(Err(v)))
    }

    fn from_ok(v: <Self as Try>::Ok) -> Self {
        v.map(|x| x.map(Ok))
    }
}

impl<T, E> Try for Poll<Result<T, E>> {
    type Ok = Poll<T>;
    type Error = E;

    #[inline]
    fn into_result(self) -> Result<Self::Ok, Self::Error> {
        match self {
            Poll::Ready(Ok(x)) => Ok(Poll::Ready(x)),
            Poll::Ready(Err(e)) => Err(e),
            Poll::Pending => Ok(Poll::Pending),
        }
    }

    #[inline]
    fn from_error(e: Self::Error) -> Self {
        Poll::Ready(Err(e))
    }

    #[inline]
    fn from_ok(x: Self::Ok) -> Self {
        x.map(Ok)
    }
}

/// Unwraps a result or propagates its error.

#[macro_export]
macro_rules! r#try {
    ($expr:expr) => {
        match $crate::Try::into_result($expr) {
            Ok(val) => val,
            Err(err) => return $crate::Try::from_error(::core::convert::From::from(err)),
        }
    };
    ($expr:expr,) => {
        $crate::r#try!($expr)
    };
}