]> git.proxmox.com Git - rustc.git/blob - src/libcore/task/poll.rs
New upstream version 1.36.0+dfsg1
[rustc.git] / src / libcore / task / poll.rs
1 #![stable(feature = "futures_api", since = "1.36.0")]
2
3 use crate::ops::Try;
4 use crate::result::Result;
5
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")]
11 pub enum Poll<T> {
12 /// Represents that a value is immediately ready.
13 #[stable(feature = "futures_api", since = "1.36.0")]
14 Ready(
15 #[stable(feature = "futures_api", since = "1.36.0")]
16 T
17 ),
18
19 /// Represents that a value is not ready yet.
20 ///
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.
24 #[stable(feature = "futures_api", since = "1.36.0")]
25 Pending,
26 }
27
28 impl<T> Poll<T> {
29 /// Changes the ready value of this `Poll` with the closure provided.
30 #[stable(feature = "futures_api", since = "1.36.0")]
31 pub fn map<U, F>(self, f: F) -> Poll<U>
32 where F: FnOnce(T) -> U
33 {
34 match self {
35 Poll::Ready(t) => Poll::Ready(f(t)),
36 Poll::Pending => Poll::Pending,
37 }
38 }
39
40 /// Returns `true` if this is `Poll::Ready`
41 #[inline]
42 #[stable(feature = "futures_api", since = "1.36.0")]
43 pub fn is_ready(&self) -> bool {
44 match *self {
45 Poll::Ready(_) => true,
46 Poll::Pending => false,
47 }
48 }
49
50 /// Returns `true` if this is `Poll::Pending`
51 #[inline]
52 #[stable(feature = "futures_api", since = "1.36.0")]
53 pub fn is_pending(&self) -> bool {
54 !self.is_ready()
55 }
56 }
57
58 impl<T, E> Poll<Result<T, E>> {
59 /// Changes the success value of this `Poll` with the closure provided.
60 #[stable(feature = "futures_api", since = "1.36.0")]
61 pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
62 where F: FnOnce(T) -> U
63 {
64 match self {
65 Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
66 Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
67 Poll::Pending => Poll::Pending,
68 }
69 }
70
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>>
74 where F: FnOnce(E) -> U
75 {
76 match self {
77 Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
78 Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
79 Poll::Pending => Poll::Pending,
80 }
81 }
82 }
83
84 #[stable(feature = "futures_api", since = "1.36.0")]
85 impl<T> From<T> for Poll<T> {
86 fn from(t: T) -> Poll<T> {
87 Poll::Ready(t)
88 }
89 }
90
91 #[stable(feature = "futures_api", since = "1.36.0")]
92 impl<T, E> Try for Poll<Result<T, E>> {
93 type Ok = Poll<T>;
94 type Error = E;
95
96 #[inline]
97 fn into_result(self) -> Result<Self::Ok, Self::Error> {
98 match self {
99 Poll::Ready(Ok(x)) => Ok(Poll::Ready(x)),
100 Poll::Ready(Err(e)) => Err(e),
101 Poll::Pending => Ok(Poll::Pending),
102 }
103 }
104
105 #[inline]
106 fn from_error(e: Self::Error) -> Self {
107 Poll::Ready(Err(e))
108 }
109
110 #[inline]
111 fn from_ok(x: Self::Ok) -> Self {
112 x.map(Ok)
113 }
114 }
115
116 #[stable(feature = "futures_api", since = "1.36.0")]
117 impl<T, E> Try for Poll<Option<Result<T, E>>> {
118 type Ok = Poll<Option<T>>;
119 type Error = E;
120
121 #[inline]
122 fn into_result(self) -> Result<Self::Ok, Self::Error> {
123 match self {
124 Poll::Ready(Some(Ok(x))) => Ok(Poll::Ready(Some(x))),
125 Poll::Ready(Some(Err(e))) => Err(e),
126 Poll::Ready(None) => Ok(Poll::Ready(None)),
127 Poll::Pending => Ok(Poll::Pending),
128 }
129 }
130
131 #[inline]
132 fn from_error(e: Self::Error) -> Self {
133 Poll::Ready(Some(Err(e)))
134 }
135
136 #[inline]
137 fn from_ok(x: Self::Ok) -> Self {
138 x.map(|x| x.map(Ok))
139 }
140 }