1 #![stable(feature = "futures_api", since = "1.36.0")]
4 use crate::ops::{self, ControlFlow};
5 use crate::result::Result;
6 use crate::task::Ready;
8 /// Indicates whether a value is available or if the current task has been
9 /// scheduled to receive a wakeup instead.
10 #[must_use = "this `Poll` may be a `Pending` variant, which should be handled"]
11 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
12 #[stable(feature = "futures_api", since = "1.36.0")]
14 /// Represents that a value is immediately ready.
16 #[stable(feature = "futures_api", since = "1.36.0")]
17 Ready(#[stable(feature = "futures_api", since = "1.36.0")] T),
19 /// Represents that a value is not ready yet.
21 /// When a function returns `Pending`, the function *must* also
22 /// ensure that the current task is scheduled to be awoken when
23 /// progress can be made.
25 #[stable(feature = "futures_api", since = "1.36.0")]
30 /// Maps a `Poll<T>` to `Poll<U>` by applying a function to a contained value.
34 /// Converts a <code>Poll<[String]></code> into a <code>Poll<[usize]></code>, consuming
37 /// [String]: ../../std/string/struct.String.html "String"
39 /// # use core::task::Poll;
40 /// let poll_some_string = Poll::Ready(String::from("Hello, World!"));
41 /// // `Poll::map` takes self *by value*, consuming `poll_some_string`
42 /// let poll_some_len = poll_some_string.map(|s| s.len());
44 /// assert_eq!(poll_some_len, Poll::Ready(13));
46 #[stable(feature = "futures_api", since = "1.36.0")]
47 pub fn map<U, F>(self, f: F) -> Poll<U>
52 Poll::Ready(t) => Poll::Ready(f(t)),
53 Poll::Pending => Poll::Pending,
57 /// Returns `true` if the poll is a [`Poll::Ready`] value.
62 /// # use core::task::Poll;
63 /// let x: Poll<u32> = Poll::Ready(2);
64 /// assert_eq!(x.is_ready(), true);
66 /// let x: Poll<u32> = Poll::Pending;
67 /// assert_eq!(x.is_ready(), false);
70 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
71 #[stable(feature = "futures_api", since = "1.36.0")]
72 pub const fn is_ready(&self) -> bool {
73 matches!(*self, Poll::Ready(_))
76 /// Returns `true` if the poll is a [`Pending`] value.
78 /// [`Pending`]: Poll::Pending
83 /// # use core::task::Poll;
84 /// let x: Poll<u32> = Poll::Ready(2);
85 /// assert_eq!(x.is_pending(), false);
87 /// let x: Poll<u32> = Poll::Pending;
88 /// assert_eq!(x.is_pending(), true);
91 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
92 #[stable(feature = "futures_api", since = "1.36.0")]
93 pub const fn is_pending(&self) -> bool {
97 /// Extracts the successful type of a [`Poll<T>`].
99 /// When combined with the `?` operator, this function will
100 /// propagate any [`Poll::Pending`] values to the caller, and
101 /// extract the `T` from [`Poll::Ready`].
106 /// #![feature(poll_ready)]
108 /// use std::task::{Context, Poll};
109 /// use std::future::{self, Future};
110 /// use std::pin::Pin;
112 /// pub fn do_poll(cx: &mut Context<'_>) -> Poll<()> {
113 /// let mut fut = future::ready(42);
114 /// let fut = Pin::new(&mut fut);
116 /// let num = fut.poll(cx).ready()?;
124 #[unstable(feature = "poll_ready", issue = "89780")]
125 pub fn ready(self) -> Ready<T> {
130 impl<T, E> Poll<Result<T, E>> {
131 /// Maps a `Poll<Result<T, E>>` to `Poll<Result<U, E>>` by applying a
132 /// function to a contained `Poll::Ready(Ok)` value, leaving all other
133 /// variants untouched.
135 /// This function can be used to compose the results of two functions.
140 /// # use core::task::Poll;
141 /// let res: Poll<Result<u8, _>> = Poll::Ready("12".parse());
142 /// let squared = res.map_ok(|n| n * n);
143 /// assert_eq!(squared, Poll::Ready(Ok(144)));
145 #[stable(feature = "futures_api", since = "1.36.0")]
146 pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
151 Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
152 Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
153 Poll::Pending => Poll::Pending,
157 /// Maps a `Poll::Ready<Result<T, E>>` to `Poll::Ready<Result<T, F>>` by
158 /// applying a function to a contained `Poll::Ready(Err)` value, leaving all other
159 /// variants untouched.
161 /// This function can be used to pass through a successful result while handling
167 /// # use core::task::Poll;
168 /// let res: Poll<Result<u8, _>> = Poll::Ready("oops".parse());
169 /// let res = res.map_err(|_| 0_u8);
170 /// assert_eq!(res, Poll::Ready(Err(0)));
172 #[stable(feature = "futures_api", since = "1.36.0")]
173 pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
178 Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
179 Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
180 Poll::Pending => Poll::Pending,
185 impl<T, E> Poll<Option<Result<T, E>>> {
186 /// Maps a `Poll<Option<Result<T, E>>>` to `Poll<Option<Result<U, E>>>` by
187 /// applying a function to a contained `Poll::Ready(Some(Ok))` value,
188 /// leaving all other variants untouched.
190 /// This function can be used to compose the results of two functions.
195 /// # use core::task::Poll;
196 /// let res: Poll<Option<Result<u8, _>>> = Poll::Ready(Some("12".parse()));
197 /// let squared = res.map_ok(|n| n * n);
198 /// assert_eq!(squared, Poll::Ready(Some(Ok(144))));
200 #[stable(feature = "poll_map", since = "1.51.0")]
201 pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
206 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))),
207 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
208 Poll::Ready(None) => Poll::Ready(None),
209 Poll::Pending => Poll::Pending,
213 /// Maps a `Poll::Ready<Option<Result<T, E>>>` to
214 /// `Poll::Ready<Option<Result<T, F>>>` by applying a function to a
215 /// contained `Poll::Ready(Some(Err))` value, leaving all other variants
218 /// This function can be used to pass through a successful result while handling
224 /// # use core::task::Poll;
225 /// let res: Poll<Option<Result<u8, _>>> = Poll::Ready(Some("oops".parse()));
226 /// let res = res.map_err(|_| 0_u8);
227 /// assert_eq!(res, Poll::Ready(Some(Err(0))));
229 #[stable(feature = "poll_map", since = "1.51.0")]
230 pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
235 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
236 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
237 Poll::Ready(None) => Poll::Ready(None),
238 Poll::Pending => Poll::Pending,
243 #[stable(feature = "futures_api", since = "1.36.0")]
244 #[rustc_const_unstable(feature = "const_convert", issue = "88674")]
245 impl<T> const From<T> for Poll<T> {
246 /// Moves the value into a [`Poll::Ready`] to make a `Poll<T>`.
251 /// # use core::task::Poll;
252 /// assert_eq!(Poll::from(true), Poll::Ready(true));
254 fn from(t: T) -> Poll<T> {
259 #[unstable(feature = "try_trait_v2", issue = "84277")]
260 impl<T, E> ops::Try for Poll<Result<T, E>> {
261 type Output = Poll<T>;
262 type Residual = Result<convert::Infallible, E>;
265 fn from_output(c: Self::Output) -> Self {
270 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
272 Poll::Ready(Ok(x)) => ControlFlow::Continue(Poll::Ready(x)),
273 Poll::Ready(Err(e)) => ControlFlow::Break(Err(e)),
274 Poll::Pending => ControlFlow::Continue(Poll::Pending),
279 #[unstable(feature = "try_trait_v2", issue = "84277")]
280 impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>> for Poll<Result<T, F>> {
282 fn from_residual(x: Result<convert::Infallible, E>) -> Self {
284 Err(e) => Poll::Ready(Err(From::from(e))),
289 #[unstable(feature = "try_trait_v2", issue = "84277")]
290 impl<T, E> ops::Try for Poll<Option<Result<T, E>>> {
291 type Output = Poll<Option<T>>;
292 type Residual = Result<convert::Infallible, E>;
295 fn from_output(c: Self::Output) -> Self {
300 fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
302 Poll::Ready(Some(Ok(x))) => ControlFlow::Continue(Poll::Ready(Some(x))),
303 Poll::Ready(Some(Err(e))) => ControlFlow::Break(Err(e)),
304 Poll::Ready(None) => ControlFlow::Continue(Poll::Ready(None)),
305 Poll::Pending => ControlFlow::Continue(Poll::Pending),
310 #[unstable(feature = "try_trait_v2", issue = "84277")]
311 impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>>
312 for Poll<Option<Result<T, F>>>
315 fn from_residual(x: Result<convert::Infallible, E>) -> Self {
317 Err(e) => Poll::Ready(Some(Err(From::from(e)))),