1#![stable(feature = "futures_api", since = "1.36.0")]
2
3use crate::convert;
4use crate::ops::{self, ControlFlow};
5
6#[must_use = "this `Poll` may be a `Pending` variant, which should be handled"]
11#[derive(#[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::marker::Copy> crate::marker::Copy for Poll<T> { }Copy, #[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::clone::Clone> crate::clone::Clone for Poll<T> {
#[inline]
fn clone(&self) -> Poll<T> {
match self {
Poll::Ready(__self_0) =>
Poll::Ready(crate::clone::Clone::clone(__self_0)),
Poll::Pending => Poll::Pending,
}
}
}Clone, #[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::fmt::Debug> crate::fmt::Debug for Poll<T> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
match self {
Poll::Ready(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f, "Ready",
&__self_0),
Poll::Pending => crate::fmt::Formatter::write_str(f, "Pending"),
}
}
}Debug, #[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::cmp::Eq> crate::cmp::Eq for Poll<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) { let _: crate::cmp::AssertParamIsEq<T>; }
}Eq, #[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::cmp::PartialEq> crate::cmp::PartialEq for Poll<T> {
#[inline]
fn eq(&self, other: &Poll<T>) -> bool {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Poll::Ready(__self_0), Poll::Ready(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::cmp::Ord> crate::cmp::Ord for Poll<T> {
#[inline]
fn cmp(&self, other: &Poll<T>) -> crate::cmp::Ordering {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
match crate::cmp::Ord::cmp(&__self_discr, &__arg1_discr) {
crate::cmp::Ordering::Equal =>
match (self, other) {
(Poll::Ready(__self_0), Poll::Ready(__arg1_0)) =>
crate::cmp::Ord::cmp(__self_0, __arg1_0),
_ => crate::cmp::Ordering::Equal,
},
cmp => cmp,
}
}
}Ord, #[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::cmp::PartialOrd> crate::cmp::PartialOrd for Poll<T> {
#[inline]
fn partial_cmp(&self, other: &Poll<T>)
-> crate::option::Option<crate::cmp::Ordering> {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
match (self, other) {
(Poll::Ready(__self_0), Poll::Ready(__arg1_0)) =>
crate::cmp::PartialOrd::partial_cmp(__self_0, __arg1_0),
_ =>
crate::cmp::PartialOrd::partial_cmp(&__self_discr,
&__arg1_discr),
}
}
}PartialOrd, #[automatically_derived]
#[stable(feature = "futures_api", since = "1.36.0")]
impl<T: crate::hash::Hash> crate::hash::Hash for Poll<T> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = crate::intrinsics::discriminant_value(self);
crate::hash::Hash::hash(&__self_discr, state);
match self {
Poll::Ready(__self_0) => crate::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash)]
12#[lang = "Poll"]
13#[stable(feature = "futures_api", since = "1.36.0")]
14pub enum Poll<T> {
15 #[lang = "Ready"]
17 #[stable(feature = "futures_api", since = "1.36.0")]
18 Ready(#[stable(feature = "futures_api", since = "1.36.0")] T),
19
20 #[lang = "Pending"]
26 #[stable(feature = "futures_api", since = "1.36.0")]
27 Pending,
28}
29
30impl<T> Poll<T> {
31 #[stable(feature = "futures_api", since = "1.36.0")]
48 #[inline]
49 pub fn map<U, F>(self, f: F) -> Poll<U>
50 where
51 F: FnOnce(T) -> U,
52 {
53 match self {
54 Poll::Ready(t) => Poll::Ready(f(t)),
55 Poll::Pending => Poll::Pending,
56 }
57 }
58
59 #[inline]
72 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
73 #[stable(feature = "futures_api", since = "1.36.0")]
74 pub const fn is_ready(&self) -> bool {
75 #[allow(non_exhaustive_omitted_patterns)] match *self {
Poll::Ready(_) => true,
_ => false,
}matches!(*self, Poll::Ready(_))
76 }
77
78 #[inline]
93 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
94 #[stable(feature = "futures_api", since = "1.36.0")]
95 pub const fn is_pending(&self) -> bool {
96 !self.is_ready()
97 }
98}
99
100impl<T, E> Poll<Result<T, E>> {
101 #[stable(feature = "futures_api", since = "1.36.0")]
116 #[inline]
117 pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
118 where
119 F: FnOnce(T) -> U,
120 {
121 match self {
122 Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
123 Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
124 Poll::Pending => Poll::Pending,
125 }
126 }
127
128 #[stable(feature = "futures_api", since = "1.36.0")]
144 #[inline]
145 pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
146 where
147 F: FnOnce(E) -> U,
148 {
149 match self {
150 Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
151 Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
152 Poll::Pending => Poll::Pending,
153 }
154 }
155}
156
157impl<T, E> Poll<Option<Result<T, E>>> {
158 #[stable(feature = "poll_map", since = "1.51.0")]
173 #[inline]
174 pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
175 where
176 F: FnOnce(T) -> U,
177 {
178 match self {
179 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))),
180 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
181 Poll::Ready(None) => Poll::Ready(None),
182 Poll::Pending => Poll::Pending,
183 }
184 }
185
186 #[stable(feature = "poll_map", since = "1.51.0")]
203 #[inline]
204 pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
205 where
206 F: FnOnce(E) -> U,
207 {
208 match self {
209 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
210 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
211 Poll::Ready(None) => Poll::Ready(None),
212 Poll::Pending => Poll::Pending,
213 }
214 }
215}
216
217#[stable(feature = "futures_api", since = "1.36.0")]
218#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
219impl<T> const From<T> for Poll<T> {
220 fn from(t: T) -> Poll<T> {
229 Poll::Ready(t)
230 }
231}
232
233#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
234impl<T, E> ops::Try for Poll<Result<T, E>> {
235 type Output = Poll<T>;
236 type Residual = Result<convert::Infallible, E>;
237
238 #[inline]
239 fn from_output(c: Self::Output) -> Self {
240 c.map(Ok)
241 }
242
243 #[inline]
244 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
245 match self {
246 Poll::Ready(Ok(x)) => ControlFlow::Continue(Poll::Ready(x)),
247 Poll::Ready(Err(e)) => ControlFlow::Break(Err(e)),
248 Poll::Pending => ControlFlow::Continue(Poll::Pending),
249 }
250 }
251}
252
253#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
254impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>> for Poll<Result<T, F>> {
255 #[inline]
256 fn from_residual(x: Result<convert::Infallible, E>) -> Self {
257 match x {
258 Err(e) => Poll::Ready(Err(From::from(e))),
259 }
260 }
261}
262
263#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
264impl<T, E> ops::Try for Poll<Option<Result<T, E>>> {
265 type Output = Poll<Option<T>>;
266 type Residual = Result<convert::Infallible, E>;
267
268 #[inline]
269 fn from_output(c: Self::Output) -> Self {
270 c.map(|x| x.map(Ok))
271 }
272
273 #[inline]
274 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
275 match self {
276 Poll::Ready(Some(Ok(x))) => ControlFlow::Continue(Poll::Ready(Some(x))),
277 Poll::Ready(Some(Err(e))) => ControlFlow::Break(Err(e)),
278 Poll::Ready(None) => ControlFlow::Continue(Poll::Ready(None)),
279 Poll::Pending => ControlFlow::Continue(Poll::Pending),
280 }
281 }
282}
283
284#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
285impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>>
286 for Poll<Option<Result<T, F>>>
287{
288 #[inline]
289 fn from_residual(x: Result<convert::Infallible, E>) -> Self {
290 match x {
291 Err(e) => Poll::Ready(Some(Err(From::from(e)))),
292 }
293 }
294}