1#![stable(feature = "futures_api", since = "1.36.0")]
2
3use crate::ops::{self, ControlFlow};
4
5#[must_use = "this `Poll` may be a `Pending` variant, which should be handled"]
10#[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::marker::StructuralPartialEq for Poll<T>
{
}
#[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)]
11#[lang = "Poll"]
12#[stable(feature = "futures_api", since = "1.36.0")]
13pub enum Poll<T> {
14 #[lang = "Ready"]
16 #[stable(feature = "futures_api", since = "1.36.0")]
17 Ready(#[stable(feature = "futures_api", since = "1.36.0")] T),
18
19 #[lang = "Pending"]
25 #[stable(feature = "futures_api", since = "1.36.0")]
26 Pending,
27}
28
29impl<T> Poll<T> {
30 #[stable(feature = "futures_api", since = "1.36.0")]
47 #[inline]
48 pub fn map<U, F>(self, f: F) -> Poll<U>
49 where
50 F: FnOnce(T) -> U,
51 {
52 match self {
53 Poll::Ready(t) => Poll::Ready(f(t)),
54 Poll::Pending => Poll::Pending,
55 }
56 }
57
58 #[inline]
71 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
72 #[stable(feature = "futures_api", since = "1.36.0")]
73 pub const fn is_ready(&self) -> bool {
74 #[allow(non_exhaustive_omitted_patterns)] match *self {
Poll::Ready(_) => true,
_ => false,
}matches!(*self, Poll::Ready(_))
75 }
76
77 #[inline]
92 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
93 #[stable(feature = "futures_api", since = "1.36.0")]
94 pub const fn is_pending(&self) -> bool {
95 !self.is_ready()
96 }
97}
98
99impl<T, E> Poll<Result<T, E>> {
100 #[stable(feature = "futures_api", since = "1.36.0")]
115 #[inline]
116 pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
117 where
118 F: FnOnce(T) -> U,
119 {
120 match self {
121 Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
122 Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
123 Poll::Pending => Poll::Pending,
124 }
125 }
126
127 #[stable(feature = "futures_api", since = "1.36.0")]
143 #[inline]
144 pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
145 where
146 F: FnOnce(E) -> U,
147 {
148 match self {
149 Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
150 Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
151 Poll::Pending => Poll::Pending,
152 }
153 }
154}
155
156impl<T, E> Poll<Option<Result<T, E>>> {
157 #[stable(feature = "poll_map", since = "1.51.0")]
172 #[inline]
173 pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
174 where
175 F: FnOnce(T) -> U,
176 {
177 match self {
178 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))),
179 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
180 Poll::Ready(None) => Poll::Ready(None),
181 Poll::Pending => Poll::Pending,
182 }
183 }
184
185 #[stable(feature = "poll_map", since = "1.51.0")]
202 #[inline]
203 pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
204 where
205 F: FnOnce(E) -> U,
206 {
207 match self {
208 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
209 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
210 Poll::Ready(None) => Poll::Ready(None),
211 Poll::Pending => Poll::Pending,
212 }
213 }
214}
215
216#[stable(feature = "futures_api", since = "1.36.0")]
217#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
218const impl<T> From<T> for Poll<T> {
219 fn from(t: T) -> Poll<T> {
228 Poll::Ready(t)
229 }
230}
231
232#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
233impl<T, E> ops::Try for Poll<Result<T, E>> {
234 type Output = Poll<T>;
235 type Residual = Result<!, E>;
236
237 #[inline]
238 fn from_output(c: Self::Output) -> Self {
239 c.map(Ok)
240 }
241
242 #[inline]
243 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
244 match self {
245 Poll::Ready(Ok(x)) => ControlFlow::Continue(Poll::Ready(x)),
246 Poll::Ready(Err(e)) => ControlFlow::Break(Err(e)),
247 Poll::Pending => ControlFlow::Continue(Poll::Pending),
248 }
249 }
250}
251
252#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
253impl<T, E, F: From<E>> ops::FromResidual<Result<!, E>> for Poll<Result<T, F>> {
254 #[inline]
255 fn from_residual(x: Result<!, E>) -> Self {
256 match x {
257 Err(e) => Poll::Ready(Err(From::from(e))),
258 }
259 }
260}
261
262#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
263impl<T, E> ops::Try for Poll<Option<Result<T, E>>> {
264 type Output = Poll<Option<T>>;
265 type Residual = Result<!, E>;
266
267 #[inline]
268 fn from_output(c: Self::Output) -> Self {
269 c.map(|x| x.map(Ok))
270 }
271
272 #[inline]
273 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
274 match self {
275 Poll::Ready(Some(Ok(x))) => ControlFlow::Continue(Poll::Ready(Some(x))),
276 Poll::Ready(Some(Err(e))) => ControlFlow::Break(Err(e)),
277 Poll::Ready(None) => ControlFlow::Continue(Poll::Ready(None)),
278 Poll::Pending => ControlFlow::Continue(Poll::Pending),
279 }
280 }
281}
282
283#[unstable(feature = "try_trait_v2", issue = "84277", old_name = "try_trait")]
284impl<T, E, F: From<E>> ops::FromResidual<Result<!, E>> for Poll<Option<Result<T, F>>> {
285 #[inline]
286 fn from_residual(x: Result<!, E>) -> Self {
287 match x {
288 Err(e) => Poll::Ready(Some(Err(From::from(e)))),
289 }
290 }
291}