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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use futures::{self, Async};
#[cfg(feature = "nightly")]
use std::ops::Try;

/// A type alias of `Poll<Result<T, E>>`.
pub type PollResult<T, E> = Poll<Result<T, E>>;

/// An enum which indicates whether a value is ready or not.
// FIXME: replace with core::task::Poll
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Poll<T> {
    /// The task has just been finished with a returned value of `T`.
    Ready(T),

    /// The task is not ready and should be scheduled to be awoken by the executor.
    Pending,
}

impl<T> Poll<T> {
    /// Return whether the value is `Pending`.
    pub fn is_pending(&self) -> bool {
        match *self {
            Poll::Pending => true,
            _ => false,
        }
    }

    /// Return whether the value is `Ready`.
    pub fn is_ready(&self) -> bool {
        !self.is_pending()
    }

    /// Maps the value to a new type with given function.
    pub fn map<F, U>(self, f: F) -> Poll<U>
    where
        F: FnOnce(T) -> U,
    {
        match self {
            Poll::Pending => Poll::Pending,
            Poll::Ready(t) => Poll::Ready(f(t)),
        }
    }
}

impl<T, E> Poll<Result<T, E>> {
    /// Return whether the value is `Ready(Ok(t))`.
    pub fn is_ok(&self) -> bool {
        match *self {
            Poll::Ready(Ok(..)) => true,
            _ => false,
        }
    }

    /// Return whether the value is `Ready(Err(t))`.
    pub fn is_err(&self) -> bool {
        match *self {
            Poll::Ready(Err(..)) => true,
            _ => false,
        }
    }

    /// Maps the value to a new type with given function if the value is `Ok`.
    pub fn map_ok<F, U>(self, f: F) -> Poll<Result<U, E>>
    where
        F: FnOnce(T) -> U,
    {
        self.map(|t| t.map(f))
    }

    /// Maps the value to a new type with given function if the value is `Err`.
    pub fn map_err<F, U>(self, f: F) -> Poll<Result<T, U>>
    where
        F: FnOnce(E) -> U,
    {
        self.map(|t| t.map_err(f))
    }
}

impl<T> From<T> for Poll<T> {
    fn from(ready: T) -> Poll<T> {
        Poll::Ready(ready)
    }
}

impl<T> From<Async<T>> for Poll<T> {
    fn from(v: Async<T>) -> Self {
        match v {
            Async::NotReady => Poll::Pending,
            Async::Ready(v) => Poll::Ready(v),
        }
    }
}

impl<T> Into<Async<T>> for Poll<T> {
    fn into(self) -> Async<T> {
        match self {
            Poll::Pending => Async::NotReady,
            Poll::Ready(v) => Async::Ready(v),
        }
    }
}

impl<T, E> From<futures::Poll<T, E>> for Poll<Result<T, E>> {
    fn from(v: futures::Poll<T, E>) -> Self {
        match v {
            Ok(Async::NotReady) => Poll::Pending,
            Ok(Async::Ready(ok)) => Poll::Ready(Ok(ok)),
            Err(err) => Poll::Ready(Err(err)),
        }
    }
}

impl<T, E> Into<futures::Poll<T, E>> for Poll<Result<T, E>> {
    fn into(self) -> futures::Poll<T, E> {
        match self {
            Poll::Pending => Ok(Async::NotReady),
            Poll::Ready(Ok(ok)) => Ok(Async::Ready(ok)),
            Poll::Ready(Err(err)) => Err(err),
        }
    }
}

#[cfg(feature = "nightly")]
impl<T, E> Try for Poll<Result<T, E>> {
    type Ok = T;
    type Error = PollError<E>;

    fn into_result(self) -> Result<Self::Ok, Self::Error> {
        match self {
            Poll::Pending => Err(PollError::Pending),
            Poll::Ready(Ok(ok)) => Ok(ok),
            Poll::Ready(Err(e)) => Err(PollError::Error(e)),
        }
    }

    fn from_ok(v: Self::Ok) -> Self {
        Poll::Ready(Ok(v))
    }

    fn from_error(v: Self::Error) -> Self {
        match v {
            PollError::Pending => Poll::Pending,
            PollError::Error(err) => Poll::Ready(Err(err)),
        }
    }
}

// An opaque type for implementation of Try
#[cfg(feature = "nightly")]
#[allow(missing_docs)]
#[allow(missing_debug_implementations)]
pub enum PollError<E> {
    Pending,
    Error(E),
}

/// A helper macro for extracting the value of `Poll<T>`.
#[macro_export]
macro_rules! poll {
    ($e:expr) => {{
        use $crate::Poll;
        match Poll::from($e) {
            Poll::Ready(v) => v,
            Poll::Pending => return Poll::Pending,
        }
    }};
}

/// A helper macro for extracting the successful value of `PollResult<T, E>`.
#[macro_export]
macro_rules! poll_result {
    ($e:expr) => {{
        use $crate::Poll;
        match Poll::from($e) {
            Poll::Ready(Ok(v)) => v,
            Poll::Ready(Err(e)) => return Poll::Ready(Err(Into::into(e))),
            Poll::Pending => return Poll::Pending,
        }
    }};
}