1use futures::{self, Async};
2#[cfg(feature = "nightly")]
3use std::ops::Try;
4
5pub type PollResult<T, E> = Poll<Result<T, E>>;
7
8#[derive(Debug, Copy, Clone, PartialEq)]
11pub enum Poll<T> {
12 Ready(T),
14
15 Pending,
17}
18
19impl<T> Poll<T> {
20 pub fn is_pending(&self) -> bool {
22 match *self {
23 Poll::Pending => true,
24 _ => false,
25 }
26 }
27
28 pub fn is_ready(&self) -> bool {
30 !self.is_pending()
31 }
32
33 pub fn map<F, U>(self, f: F) -> Poll<U>
35 where
36 F: FnOnce(T) -> U,
37 {
38 match self {
39 Poll::Pending => Poll::Pending,
40 Poll::Ready(t) => Poll::Ready(f(t)),
41 }
42 }
43}
44
45impl<T, E> Poll<Result<T, E>> {
46 pub fn is_ok(&self) -> bool {
48 match *self {
49 Poll::Ready(Ok(..)) => true,
50 _ => false,
51 }
52 }
53
54 pub fn is_err(&self) -> bool {
56 match *self {
57 Poll::Ready(Err(..)) => true,
58 _ => false,
59 }
60 }
61
62 pub fn map_ok<F, U>(self, f: F) -> Poll<Result<U, E>>
64 where
65 F: FnOnce(T) -> U,
66 {
67 self.map(|t| t.map(f))
68 }
69
70 pub fn map_err<F, U>(self, f: F) -> Poll<Result<T, U>>
72 where
73 F: FnOnce(E) -> U,
74 {
75 self.map(|t| t.map_err(f))
76 }
77}
78
79impl<T> From<T> for Poll<T> {
80 fn from(ready: T) -> Poll<T> {
81 Poll::Ready(ready)
82 }
83}
84
85impl<T> From<Async<T>> for Poll<T> {
86 fn from(v: Async<T>) -> Self {
87 match v {
88 Async::NotReady => Poll::Pending,
89 Async::Ready(v) => Poll::Ready(v),
90 }
91 }
92}
93
94impl<T> Into<Async<T>> for Poll<T> {
95 fn into(self) -> Async<T> {
96 match self {
97 Poll::Pending => Async::NotReady,
98 Poll::Ready(v) => Async::Ready(v),
99 }
100 }
101}
102
103impl<T, E> From<futures::Poll<T, E>> for Poll<Result<T, E>> {
104 fn from(v: futures::Poll<T, E>) -> Self {
105 match v {
106 Ok(Async::NotReady) => Poll::Pending,
107 Ok(Async::Ready(ok)) => Poll::Ready(Ok(ok)),
108 Err(err) => Poll::Ready(Err(err)),
109 }
110 }
111}
112
113impl<T, E> Into<futures::Poll<T, E>> for Poll<Result<T, E>> {
114 fn into(self) -> futures::Poll<T, E> {
115 match self {
116 Poll::Pending => Ok(Async::NotReady),
117 Poll::Ready(Ok(ok)) => Ok(Async::Ready(ok)),
118 Poll::Ready(Err(err)) => Err(err),
119 }
120 }
121}
122
123#[cfg(feature = "nightly")]
124impl<T, E> Try for Poll<Result<T, E>> {
125 type Ok = T;
126 type Error = PollError<E>;
127
128 fn into_result(self) -> Result<Self::Ok, Self::Error> {
129 match self {
130 Poll::Pending => Err(PollError::Pending),
131 Poll::Ready(Ok(ok)) => Ok(ok),
132 Poll::Ready(Err(e)) => Err(PollError::Error(e)),
133 }
134 }
135
136 fn from_ok(v: Self::Ok) -> Self {
137 Poll::Ready(Ok(v))
138 }
139
140 fn from_error(v: Self::Error) -> Self {
141 match v {
142 PollError::Pending => Poll::Pending,
143 PollError::Error(err) => Poll::Ready(Err(err)),
144 }
145 }
146}
147
148#[cfg(feature = "nightly")]
150#[allow(missing_docs)]
151#[allow(missing_debug_implementations)]
152pub enum PollError<E> {
153 Pending,
154 Error(E),
155}
156
157#[macro_export]
159macro_rules! poll {
160 ($e:expr) => {{
161 use $crate::Poll;
162 match Poll::from($e) {
163 Poll::Ready(v) => v,
164 Poll::Pending => return Poll::Pending,
165 }
166 }};
167}
168
169#[macro_export]
171macro_rules! poll_result {
172 ($e:expr) => {{
173 use $crate::Poll;
174 match Poll::from($e) {
175 Poll::Ready(Ok(v)) => v,
176 Poll::Ready(Err(e)) => return Poll::Ready(Err(Into::into(e))),
177 Poll::Pending => return Poll::Pending,
178 }
179 }};
180}