]> git.proxmox.com Git - rustc.git/blame - library/core/src/task/poll.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / core / src / task / poll.rs
CommitLineData
48663c56 1#![stable(feature = "futures_api", since = "1.36.0")]
8faf50e0 2
48663c56
XL
3use crate::ops::Try;
4use crate::result::Result;
8faf50e0
XL
5
6/// Indicates whether a value is available or if the current task has been
7/// scheduled to receive a wakeup instead.
9fa01778 8#[must_use = "this `Poll` may be a `Pending` variant, which should be handled"]
8faf50e0 9#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
48663c56 10#[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0
XL
11pub enum Poll<T> {
12 /// Represents that a value is immediately ready.
1b1a35ee 13 #[lang = "Ready"]
48663c56 14 #[stable(feature = "futures_api", since = "1.36.0")]
60c5eb7d 15 Ready(#[stable(feature = "futures_api", since = "1.36.0")] T),
8faf50e0
XL
16
17 /// Represents that a value is not ready yet.
18 ///
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.
1b1a35ee 22 #[lang = "Pending"]
48663c56 23 #[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0
XL
24 Pending,
25}
26
27impl<T> Poll<T> {
9fa01778 28 /// Changes the ready value of this `Poll` with the closure provided.
48663c56 29 #[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0 30 pub fn map<U, F>(self, f: F) -> Poll<U>
60c5eb7d
XL
31 where
32 F: FnOnce(T) -> U,
8faf50e0
XL
33 {
34 match self {
35 Poll::Ready(t) => Poll::Ready(f(t)),
36 Poll::Pending => Poll::Pending,
37 }
38 }
39
9fa01778 40 /// Returns `true` if this is `Poll::Ready`
8faf50e0 41 #[inline]
48663c56 42 #[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0 43 pub fn is_ready(&self) -> bool {
dfeec247 44 matches!(*self, Poll::Ready(_))
8faf50e0
XL
45 }
46
9fa01778 47 /// Returns `true` if this is `Poll::Pending`
8faf50e0 48 #[inline]
48663c56 49 #[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0
XL
50 pub fn is_pending(&self) -> bool {
51 !self.is_ready()
52 }
53}
54
55impl<T, E> Poll<Result<T, E>> {
9fa01778 56 /// Changes the success value of this `Poll` with the closure provided.
48663c56 57 #[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0 58 pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
60c5eb7d
XL
59 where
60 F: FnOnce(T) -> U,
8faf50e0
XL
61 {
62 match self {
63 Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
64 Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
65 Poll::Pending => Poll::Pending,
66 }
67 }
68
9fa01778 69 /// Changes the error value of this `Poll` with the closure provided.
48663c56 70 #[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0 71 pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
60c5eb7d
XL
72 where
73 F: FnOnce(E) -> U,
8faf50e0
XL
74 {
75 match self {
76 Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
77 Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
78 Poll::Pending => Poll::Pending,
79 }
80 }
81}
82
e1599b0c
XL
83impl<T, E> Poll<Option<Result<T, E>>> {
84 /// Changes the success value of this `Poll` with the closure provided.
85 #[unstable(feature = "poll_map", issue = "63514")]
86 pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
60c5eb7d
XL
87 where
88 F: FnOnce(T) -> U,
e1599b0c
XL
89 {
90 match self {
91 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))),
92 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
93 Poll::Ready(None) => Poll::Ready(None),
94 Poll::Pending => Poll::Pending,
95 }
96 }
97
98 /// Changes the error value of this `Poll` with the closure provided.
99 #[unstable(feature = "poll_map", issue = "63514")]
100 pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
60c5eb7d
XL
101 where
102 F: FnOnce(E) -> U,
e1599b0c
XL
103 {
104 match self {
105 Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
106 Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
107 Poll::Ready(None) => Poll::Ready(None),
108 Poll::Pending => Poll::Pending,
109 }
110 }
111}
112
48663c56 113#[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0 114impl<T> From<T> for Poll<T> {
1b1a35ee
XL
115 /// Convert to a `Ready` variant.
116 ///
117 /// # Example
118 ///
119 /// ```
120 /// # use core::task::Poll;
121 /// assert_eq!(Poll::from(true), Poll::Ready(true));
122 /// ```
8faf50e0
XL
123 fn from(t: T) -> Poll<T> {
124 Poll::Ready(t)
125 }
126}
127
48663c56 128#[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0
XL
129impl<T, E> Try for Poll<Result<T, E>> {
130 type Ok = Poll<T>;
131 type Error = E;
132
133 #[inline]
134 fn into_result(self) -> Result<Self::Ok, Self::Error> {
135 match self {
136 Poll::Ready(Ok(x)) => Ok(Poll::Ready(x)),
137 Poll::Ready(Err(e)) => Err(e),
138 Poll::Pending => Ok(Poll::Pending),
139 }
140 }
141
142 #[inline]
143 fn from_error(e: Self::Error) -> Self {
144 Poll::Ready(Err(e))
145 }
146
147 #[inline]
148 fn from_ok(x: Self::Ok) -> Self {
149 x.map(Ok)
150 }
151}
152
48663c56 153#[stable(feature = "futures_api", since = "1.36.0")]
8faf50e0
XL
154impl<T, E> Try for Poll<Option<Result<T, E>>> {
155 type Ok = Poll<Option<T>>;
156 type Error = E;
157
158 #[inline]
159 fn into_result(self) -> Result<Self::Ok, Self::Error> {
160 match self {
161 Poll::Ready(Some(Ok(x))) => Ok(Poll::Ready(Some(x))),
162 Poll::Ready(Some(Err(e))) => Err(e),
163 Poll::Ready(None) => Ok(Poll::Ready(None)),
164 Poll::Pending => Ok(Poll::Pending),
165 }
166 }
167
168 #[inline]
169 fn from_error(e: Self::Error) -> Self {
170 Poll::Ready(Some(Err(e)))
171 }
172
173 #[inline]
174 fn from_ok(x: Self::Ok) -> Self {
175 x.map(|x| x.map(Ok))
176 }
177}