1 #![stable(feature = "futures_api", since = "1.36.0")]
4 use crate::result::Result;
6 /// Indicates whether a value is available or if the current task has been
7 /// scheduled to receive a wakeup instead.
8 #[must_use = "this `Poll` may be a `Pending` variant, which should be handled"]
9 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
10 #[stable(feature = "futures_api", since = "1.36.0")]
12 /// Represents that a value is immediately ready.
14 #[stable(feature = "futures_api", since = "1.36.0")]
15 Ready(#[stable(feature = "futures_api", since = "1.36.0")] T),
17 /// Represents that a value is not ready yet.
19 /// When a function returns `Pending`, the function *must* also
20 /// ensure that the current task is scheduled to be awoken when
21 /// progress can be made.
23 #[stable(feature = "futures_api", since = "1.36.0")]
28 /// Changes the ready value of this `Poll` with the closure provided.
29 #[stable(feature = "futures_api", since = "1.36.0")]
30 pub fn map<U, F>(self, f: F) -> Poll<U>
35 Poll::Ready(t) => Poll::Ready(f(t)),
36 Poll::Pending => Poll::Pending,
40 /// Returns `true` if this is `Poll::Ready`
42 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
43 #[stable(feature = "futures_api", since = "1.36.0")]
44 pub const fn is_ready(&self) -> bool {
45 matches!(*self, Poll::Ready(_))
48 /// Returns `true` if this is `Poll::Pending`
50 #[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
51 #[stable(feature = "futures_api", since = "1.36.0")]
52 pub const fn is_pending(&self) -> bool {
57 impl<T, E> Poll<Result<T, E>> {
58 /// Changes the success value of this `Poll` with the closure provided.
59 #[stable(feature = "futures_api", since = "1.36.0")]
60 pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
65 Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
66 Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
67 Poll::Pending => Poll::Pending,
71 /// Changes the error value of this `Poll` with the closure provided.
72 #[stable(feature = "futures_api", since = "1.36.0")]
73 pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
78 Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
79 Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
80 Poll::Pending => Poll::Pending,
85 impl<T, E> Poll<Option<Result<T, E>>> {
86 /// Changes the success value of this `Poll` with the closure provided.
87 #[stable(feature = "poll_map", since = "1.51.0")]
88 pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
93 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))),
94 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
95 Poll::Ready(None) => Poll::Ready(None),
96 Poll::Pending => Poll::Pending,
100 /// Changes the error value of this `Poll` with the closure provided.
101 #[stable(feature = "poll_map", since = "1.51.0")]
102 pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
107 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
108 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
109 Poll::Ready(None) => Poll::Ready(None),
110 Poll::Pending => Poll::Pending,
115 #[stable(feature = "futures_api", since = "1.36.0")]
116 impl<T> From<T> for Poll<T> {
117 /// Convert to a `Ready` variant.
122 /// # use core::task::Poll;
123 /// assert_eq!(Poll::from(true), Poll::Ready(true));
125 fn from(t: T) -> Poll<T> {
130 #[stable(feature = "futures_api", since = "1.36.0")]
131 impl<T, E> Try for Poll<Result<T, E>> {
136 fn into_result(self) -> Result<Self::Ok, Self::Error> {
138 Poll::Ready(Ok(x)) => Ok(Poll::Ready(x)),
139 Poll::Ready(Err(e)) => Err(e),
140 Poll::Pending => Ok(Poll::Pending),
145 fn from_error(e: Self::Error) -> Self {
150 fn from_ok(x: Self::Ok) -> Self {
155 #[stable(feature = "futures_api", since = "1.36.0")]
156 impl<T, E> Try for Poll<Option<Result<T, E>>> {
157 type Ok = Poll<Option<T>>;
161 fn into_result(self) -> Result<Self::Ok, Self::Error> {
163 Poll::Ready(Some(Ok(x))) => Ok(Poll::Ready(Some(x))),
164 Poll::Ready(Some(Err(e))) => Err(e),
165 Poll::Ready(None) => Ok(Poll::Ready(None)),
166 Poll::Pending => Ok(Poll::Pending),
171 fn from_error(e: Self::Error) -> Self {
172 Poll::Ready(Some(Err(e)))
176 fn from_ok(x: Self::Ok) -> Self {